繁体   English   中英

从父类访问子变量?

[英]Accessing child variables from parent class?

我该怎么做呢?

class test
{
    public static function run() {
        echo "Number is: ".$num;
    }
} 

class testchild extends test
{
    protected static $num = 5;
    public static function exec()  {
        $num = 5;
        parent::run();
    }
}

testchild::exec(); 说未定义的变量“num”。

http://ideone.com/WM7tHk

我如何访问此变量?

你不应该这样做,因为你要求父母访问它可能或不存在的东西。

最简单的方法是在内部声明$num 否则,您需要采取措施,通过提供(例如)受保护的抽象静态getter来保证系统信息在那里。

abstract class test
{
    public static function run() {
        echo "Number is: ".static::getNum();
    }
    protected abstract static function getNum();
}

class testchild extends test
{
    protected static $num;
    public static function exec()  {
        static::$num = 5;
        parent::run();
    }
    protected static function getNum() {
        return static::$num;
    }
}

class anotherchild extends test
{
    public static function exec()  {
        parent::run();
    }
    // We always return 42. Or maybe the Unix timestamp, who knows.
    protected static function getNum() {
        return 42;
    }
}


$c = new testchild();
$c->exec();

传递几个变量

另一种不太可靠的方法是拥有一个“通信对象”并传递对它的引用 这可以使用属性数组或( 更好 )已知结构的对象以与上面相同的方式完成:

abstract class test
{
    public static function run() {
        echo "Number is: ".static::tell('num');
    }
    protected abstract static function tell($what);

    // A concrete function to initialize the known object would be nice here.
}

class testchild extends test
{
    protected static $info; // This is an array, out of laziness.
                            // It ought to be converted to an object.

    public static function exec()  {
        static::$info['num'] = 5;   // Typo here == possibly subtle bug.
        parent::run();
    }
    protected static function tell($what) {
        return static::$info[$what];  // array_key_exists would be nice here.
    }
}

小改进

为了确保在通信时每个对象都在同一块板上,您可以使用setter抽象通信对象(现在它也可以是一个数组):

    public static function exec()  {
        static::say('num', 5);
        parent::run();
    }
    protected static function say($what, $value) {
        // array_key_exists would be nice here too.
        static::$info[$what] = $value;
    }

然后初始化会将对象的键设置为默认值,并且尝试设置不存在的键可能会引发异常。 当然,您需要仔细规划需要在不同子类中设置哪些信息,以及如何; 这实际上并不是一个好习惯,因为现在的变化往往会从孩子到父母,然后是兄弟姐妹。

class test
{
    public static function run() {
        $c = get_called_class(); //get child class variable in parent class
        echo "Number is: ".$c::$num;
    }
}

class testchild extends test
{
    protected static $num = 5;
    public static function exec()  {
        $num = 5;
        parent::run();
    }
}
testchild::exec();

输出:数量是:5

您已在父类静态方法中使用get_called_class()函数获取子类变量。

更多信息: http//php.net/manual/en/function.get-called-class.php

您的父类应该包含它希望访问的变量,例如:

class test
{
    protected static $num;
    public static function run()
    {
        echo "Number is: ".self::$num;
    }
}

class testchild extends test
{
    public static function exec()
    {
        self::$num = 5;
        parent::run();
    }
}

testchild::exec();

这里需要protected限定符。 这表示该变量只能由当前类及其后代访问 另一方面,私人不能被后代访问。

暂无
暂无

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

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