Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php类-如何使类知道父值_Php_Class - Fatal编程技术网

Php类-如何使类知道父值

Php类-如何使类知道父值,php,class,Php,Class,我怎样才能打印“ok” 我的意思是,第二个类应该知道第一个类将什么设置为结果,请参见?替换$this->result='ok'带有self::$result='ok'在一级构造函数中 顺便说一句,代码很糟糕。您混合使用静态变量和实例变量,并扩展类,但不使用扩展提供的好处。替换$this->result='ok'带有self::$result='ok'在一级构造函数中 顺便说一句,代码很糟糕。您混合了静态变量和实例变量,并扩展了类,但没有使用扩展提供的好处。您需要在第一个类中将静态引用为self:

我怎样才能打印“ok”


我的意思是,第二个类应该知道第一个类将什么设置为结果,请参见?

替换
$this->result='ok'带有
self::$result='ok'
一级构造函数中


顺便说一句,代码很糟糕。您混合使用静态变量和实例变量,并扩展类,但不使用扩展提供的好处。

替换
$this->result='ok'带有
self::$result='ok'
一级构造函数中


顺便说一句,代码很糟糕。您混合了静态变量和实例变量,并扩展了类,但没有使用扩展提供的好处。

您需要在第一个类中将静态引用为self::$result

下面应该做你想做的

<?php

class FirstClass{
    public static $second;  
    public static $result = 'not this =/';
    public function __construct(){
        $this->result = 'ok';
        $this->second = new SecondClass();
    }   

    public function show(){
        echo $this->second->value;
    }
}

class SecondClass extends FirstClass{
    public $value;
    public function __construct(){
        $this->value = parent::$result; //Make it get "ok" here
    }
}

$temp = new FirstClass();
$temp->show(); //It will show: "not this =/"

?>

您需要在第一个类中将static引用为self::$result

下面应该做你想做的

<?php

class FirstClass{
    public static $second;  
    public static $result = 'not this =/';
    public function __construct(){
        $this->result = 'ok';
        $this->second = new SecondClass();
    }   

    public function show(){
        echo $this->second->value;
    }
}

class SecondClass extends FirstClass{
    public $value;
    public function __construct(){
        $this->value = parent::$result; //Make it get "ok" here
    }
}

$temp = new FirstClass();
$temp->show(); //It will show: "not this =/"

?>