代码之家  ›  专栏  ›  技术社区  ›  datasn.io

如何从父类函数访问子类中定义的常量?

  •  28
  • datasn.io  · 技术社区  · 15 年前

    我在php.net上看到了这个例子:

    <?php
    class MyClass {
    
         const MY_CONST = "yonder";
    
         public function __construct() {
    
              $c = get_class( $this );
              echo $c::MY_CONST;
         }
    }
    
    class ChildClass extends MyClass {
    
         const MY_CONST = "bar";
    }
    
    $x = new ChildClass(); // prints 'bar'
    $y = new MyClass(); // prints 'yonder'
    ?>
    

    但是$c::my_const只能在5.3.0或更高版本中识别。我正在写的这门课可能分布很多。

    基本上,我在childClass中定义了一个常量,而myClass(父类)中的一个函数需要使用该常量。有什么想法吗?

    5 回复  |  直到 7 年前
        1
  •  76
  •   pevik Hudson    12 年前

    用一下怎么样 static::MY_CONST ?

        2
  •  7
  •   pevik Hudson    7 年前

    从PHP5.3开始:

    使用 static::MY_CONST


    更多详细信息 static

    在这种情况下 the keyword static 是对实际调用的类的引用。

    这说明了 static $var , static::$var 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
    

    还要注意,关键字 静止的 有两种完全不同的含义:

    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;
        }
    }
    
        3
  •  4
  •   Chris    15 年前

    而不是

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

    这样做

    $c = get_class( $this );
    echo constant($c . '::MY_CONST');
    
        4
  •  0
  •   Cetra    15 年前

    我无法让它与const一起工作,因为它打印“yonderyonder”(这是关于常量的东西,它们不会改变),但它与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'
    
    ?>
    
        5
  •  0
  •   Nazariy    12 年前

    如果需要访问可以使用的类或对象的常量、属性、方法 reflection ,它提供了有关对象结构的更多详细信息。
    例子:

    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