Search code examples
phpclassoopdefinedclass-constants

How to use defined() method with 'Class Constants' that are defined into a specific class in PHP?


Following is the small code snippet of a class code:

<?php
/**
 * [PHPFOX_HEADER]
 */

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
     */ 
    const PW_AUTH        = 'xxxxxxxx';
    const PW_APPLICATION = 'XXXX-XXX';
    const PW_DEBUG       = true;

    public function __construct()
    {   
        $this->_sTable = Phpfox::getT('notification');
    }
    public function pwCall() {


    if (defined('PW_DEBUG') && self::PW_DEBUG) { // Is this a right way to use defined() method for class constants? 
      print "[PW] request: Request come\n";
      print "[PW] response: Response sent\n";
    }
  } 
}    
?>

I've put in a comment into my code where I've a doubt. Please tell me is it a right way to use defined() function for class constants according to the coding standards?

Thanks.


Solution

  • You could use self:: or static:: to resolve the class constant in defined():

    if (defined('self::PW_DEBUG') && self::PW_DEBUG) {
    }