繁体   English   中英

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

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

我从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(父亲类)中的一个函数需要使用该常量。 任何的想法?

如何使用static::MY_CONST呢?

自php 5.3起:

使用static::MY_CONST


有关static更多详细信息

在这种情况下,关键字static是对实际调用的类的引用。

这说明了static $varstatic::$varself::$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

另请注意,关键字static具有2个完全不同的含义:

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

代替

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

做这个

$c = get_class( $this );
echo constant($c . '::MY_CONST');

如果需要访问常量,属性,类或对象的方法,则可以使用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

我不能让它与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'

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM