Search code examples
phpmodel-view-controllermodelconstantsphpfox

Can the constants be defined inside a model file of a framework in PHP?


I'm using PHPFox framework. I've to define two constants which would be used only by two functions present within that model class file. So can I define the constants at the beginning of this model class file or would it cause any issue or is it against the coding standards?

Please help me in this regard.

Following is one method from this model class file.

I want to write following code :

<?php
/**
 * [PHPFOX_HEADER]
 */
/*header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');*/

defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright       [PHPFOX_COPYRIGHT]
 * @author          Raymond Benc
 * @package         Phpfox_Service
 * @version         $Id: service.class.php 67 2009-01-20 11:32:45Z Raymond_Benc $
 */
class Notification_Service_Process extends Phpfox_Service 
{
/**
     * Class constructor
     */ 
    public function __construct()
    {   
        $this->_sTable = Phpfox::getT('notification');
    }

    public function add($sType, $iItemId, $iOwnerUserId, $iSenderUserId = null)
    {
        if ($iOwnerUserId == Phpfox::getUserId()&&$iSenderUserId==null)
        {
            return true;
        }

        if ($sPlugin = Phpfox_Plugin::get('notification.service_process_add'))
        {
            eval($sPlugin);
        }       

        if (isset($bDoNotInsert) || defined('SKIP_NOTIFICATION'))
        {
            return true;
        }

        $aInsert = array(
            'type_id' => $sType,
            'item_id' => $iItemId,
            'user_id' => $iOwnerUserId, 
            'owner_user_id' => ($iSenderUserId === null ? Phpfox::getUserId() : $iSenderUserId),
            'time_stamp' => PHPFOX_TIME     
        );  
        $this->database()->insert($this->_sTable, $aInsert);    

        return true;
    }
}
?>  

I want to define following two constants:

define('PW_AUTH', '8s4QpeUyLX9BodAy');
define('PW_APPLICATION', 'R8T89-29690');

Thanks in advance.


Solution

  • Set them as constants in the class.

    From the PHP manual:

    <?php
    class MyClass
    {
        const CONSTANT = 'constant value';
    
        function showConstant() {
            echo  self::CONSTANT . "\n";
        }
    }