How to access constant defined in child class from parent class functions?

后端 未结 5 1580
时光说笑
时光说笑 2021-02-03 18:37

I saw this example from php.net:



        
相关标签:
5条回答
  • 2021-02-03 18:47

    How about using static::MY_CONST?

    0 讨论(0)
  • 2021-02-03 18:51

    I couldn't get it to work with const as it prints "yonderyonder" (that's the thing about constants, they don't change), but it works fine with var:

    <?php
    class MyClass {
    
         var $MY_CONST = "yonder";
    
         public function __construct() {
    
         echo $this->MY_CONST;
         }
    }
    
    class ChildClass extends MyClass {
    
         var $MY_CONST = "bar";
    }
    
    $x = new ChildClass(); // prints 'bar'
    $y = new MyClass(); // prints 'yonder'
    
    ?>
    
    0 讨论(0)
  • 2021-02-03 18:53

    Instead of

    $c = get_class( $this );
    echo $c::MY_CONST;
    

    Do this

    $c = get_class( $this );
    echo constant($c . '::MY_CONST');
    
    0 讨论(0)
  • 2021-02-03 18:55

    Since php 5.3:

    Use static::MY_CONST


    More details on static

    In this case the keyword static is a reference to the actually called class.

    This illustrates the difference between static $var, static::$var and self::$var:

    class Base {
        const VALUE = 'base';
    
        static function testSelf() {
            // Output is always 'base', because `self::` is always class Base
            return self::VALUE;
        }
    
        static function testStatic() {
            // Output is variable: `static::` is a reference to the called class.
            return static::VALUE;
        }
    }
    
    class Child extends Base {
        const VALUE = 'child';
    }
    
    echo Base::testStatic();  // output: base
    echo Base::testSelf();    // output: base
    
    echo Child::testStatic(); // output: child
    echo Child::testSelf();   // output: base
    

    Also note that the keyword static has 2 quite different meanings:

    class StaticDemo {
        static function demo() {
            // Type 1: `static` defines a static variable.
            static $Var = 'bar';
    
            // Type 2: `static::` is a reference to the called class.
            return static::VALUE;
        }
    }
    
    0 讨论(0)
  • 2021-02-03 19:03

    If you need to access constants, properties, methods of classes or objects you can use reflection, it provides much more details about structure of the object.
    example:

    class MainClass
    {
        const name = 'Primary';
    
        public $foo = 'Foo Variable';
    }
    class ExtendedClass extends MainClass
    {
        const name = 'Extended';
    }
    
    /**
     * From Class Name
     */
    
    //get reflection of main class
    $mainReflection = new ReflectionClass('MainClass');
    
    if($mainReflection->hasConstant('name'))
        var_dump($mainReflection->getConstant('name'));//Primary
    
    //get reflection of extended class
    $extendedReflection = new ReflectionClass('ExtendedClass');
    
    if($extendedReflection->hasConstant('name'))
        var_dump($extendedReflection->getConstant('name'));//Extended
    
    /**
     * From Objects
     */
    $main = new MainClass();
    $extended = new ExtendedClass();
    
    //get reflection of main class
    $mainReflection = new ReflectionObject($main);
    
    if($mainReflection->hasConstant('name'))
        var_dump($mainReflection->getConstant('name'));//Primary
    
    //get reflection of extended class
    $extendedReflection = new ReflectionObject($extended);
    
    if($extendedReflection->hasConstant('name'))
        var_dump($extendedReflection->getConstant('name'));//Extended
    
    0 讨论(0)
提交回复
热议问题