简体   繁体   English

从父类访问子变量?

[英]Accessing child variables from parent class?

How do I do this? 我该怎么做呢?

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(); says Undefined variable "num". 说未定义的变量“num”。

http://ideone.com/WM7tHk http://ideone.com/WM7tHk

How do I access this variable? 我如何访问此变量?

You shouldn't be able to do this, because you're requesting the parent to access something that it might be or not be there. 你不应该这样做,因为你要求父母访问它可能或不存在的东西。

The simplest way would be to declare $num inside the parent . 最简单的方法是在内部声明$num Otherwise you need to take measures to guarantee the system that the information will be there, by supplying (eg) a protected abstract static getter. 否则,您需要采取措施,通过提供(例如)受保护的抽象静态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();

Passing several variables 传递几个变量

Another, less solid approach would be to have a "communication object" and pass a reference to it. 另一种不太可靠的方法是拥有一个“通信对象”并传递对它的引用 This can be done the same way as above, using a property array or ( better ) an object of known structure: 这可以使用属性数组或( 更好 )已知结构的对象以与上面相同的方式完成:

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

Small improvement 小改进

To ensure that every object is on the same board when communicating, you can abstract the communication object (now it could also be an array) with a setter : 为了确保在通信时每个对象都在同一块板上,您可以使用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;
    }

Then the initialization would set the object's keys to default values, and trying to set nonexisting keys could raise an exception. 然后初始化会将对象的键设置为默认值,并且尝试设置不存在的键可能会引发异常。 Of course you need to carefully plan what information needs to be set in the different child classes, and how; 当然,您需要仔细规划需要在不同子类中设置哪些信息,以及如何; which isn't really a good practice, as changes would now tend to cascade from a child to the parent and thence to the siblings. 这实际上并不是一个好习惯,因为现在的变化往往会从孩子到父母,然后是兄弟姐妹。

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();

Output: Number is: 5 输出:数量是:5

You have got the child class variable using get_called_class () function in a parent class static method. 您已在父类静态方法中使用get_called_class()函数获取子类变量。

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

Your parent class should instead contain the variables which it wishes to access, so, for example: 您的父类应该包含它希望访问的变量,例如:

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();

Here, the protected qualifier is required. 这里需要protected限定符。 This denotes that the variable can only be accessed by the current class and its descendants . 这表示该变量只能由当前类及其后代访问 Private, on the other hand, cannot be accessed by descendants. 另一方面,私人不能被后代访问。

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

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