PHP의 특정 클래스에 정의된 '클래스 상수'와 함께 defined() 메서드를 사용하는 방법은 무엇입니까? (How to use defined() method with 'Class Constants' that are defined into a specific class in PHP?)


문제 설명

PHP의 특정 클래스에 정의된 '클래스 상수'와 함께 defined() 메서드를 사용하는 방법은 무엇입니까? (How to use defined() method with 'Class Constants' that are defined into a specific class in PHP?)

다음은 클래스 코드의 작은 코드입니다.

<?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";
    }
  } 
}    
?>

내 코드에 의심스러운 부분에 주석을 달았습니다. 코딩 표준에 따라 클래스 상수에 대해 defined() 함수를 사용하는 것이 올바른 방법인지 알려주세요.

감사합니다.


참조 솔루션

방법 1:

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

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

(by PHPLoverJa͢ck)

참조 문서

  1. How to use defined() method with 'Class Constants' that are defined into a specific class in PHP? (CC BY‑SA 2.5/3.0/4.0)

#PHP #class-constants #class #defined #OOP






관련 질문

bash의 rsync가 php 생성 --exclude-from 파일을 구문 분석하지 않음 (rsync in bash not parsing php-generated --exclude-from file)

PHP 배열 값 저장 (PHP Save array value)

검색으로 배열에서 특정 데이터 가져오기 (get specific datas from a array by a search)

창 서비스를 사용하여 PHP 파일 트리거 (Trigger a php file using window service)

yii2 컨트롤러 작업 주입은 어떻게 작동합니까? (How does the yii2 Controller action injection works)

php와 drupal을 사용하여 pdf를 텍스트로 변환 (pdf to text convert using php and drupal)

PHP에서 카테고리 및 하위 카테고리 목록 검색 (Retrieve Category & Subcategory list in PHP)

PDO - COUNT(*) 결과를 얻습니까? (PDO - get the result of a COUNT(*)?)

PHP - MySQL 쿼리 문제 (PHP - Mysql query problem)

제품용 Reviews.io API의 Foreach 루프 (Foreach loop in Reviews.io API for Products)

숫자를 나누고 점 뒤에 하나의 숫자를 유지하십시오. (Split the number and keep one number after the dot)

내 메시지 입력이 데이터베이스에 들어가지 않습니다. (My message input doesn't get into database)







코멘트