简体   繁体   English

PHP-在子类和父类之间传递变量

[英]PHP - pass variable between child and parent class

I would to know what is the best way to pass variable between parent and child class and updating this class all along the class is executed. 我想知道在父类和子类之间传递变量并在整个类中执行更新该类的最佳方法是什么。 For example I have this parent class which execute is child class inside it: 例如,我有一个父类,其中执行的是子类:

class My_Class {

   public $data;
   public $data2;

   public function __construct() {  
   }

   public function output() {
       $data['key1'] = 1;
       $data['key2'] = 2;
       $data2['key1'] = 'a';
       $data2['key2'] = 'b';
       $child_class = new Child_Class();
       $child_class->output();
       print_r($this->data); // only contains  key1 & key2, I want to get key3 and 4 also
       print_r($this->data2);
   }

}

class Child_Class extends My_Class {

   public function __construct() {  
   }

   public function output() {
       $data  = parent::$data; // want to get data array but it's empty
       $data2 = parent::$data2; // want to get data2 array but it's empty
       this->set_data();
   }

   public function set_data() {
       $this->data['key3'] = 3;
       $this->data['key4'] = 4;
       $this->data['key3'] = 'c';
       $this->data['key4'] = 'd';
   }


}

$class = new My_class();
$class->output();

Currently I execute the child class inside the parent class because I need to populate the main data of the parent class. 当前,我需要在父类中执行子类,因为我需要填充父类的主数据。 This class will execute child class based on some variable. 此类将基于某个变量执行子类。

What is the right way to inherit and assign variable from parent to child and child to parent. 从父代到子代以及从子代到父代继承和分配变量的正确方法是什么? If I use dependency injection to retrieve the data in the extends class how can i assign the variable to the parent class? 如果我使用依赖注入来获取扩展类中的数据,如何将变量分配给父类?

"Do you have an example" - here you go.... “您有例子吗?”-您来这里...

<?php
class My_Class {
    public $data = array('key1'=>1, 'key2'=>2);
    public $data2 = array('key1'=>'a', 'key2'=>'b');

    public function output() {
        echo "MyClass::output\r\n";
        print_r($this->data);
        print_r($this->data2);
    }
}


class Child_Class extends My_Class {

    public function __construct() {
        $this->data['key3'] = 3;
        $this->data['key4'] = 4;
        $this->data2['key3'] = 'c';
        $this->data2['key4'] = 'd';
    }

    public function output() {
        echo "Child_Class::output\r\n";
        parent::output();
    }
}

$class = new Child_Class();
$class->output();

prints 版画

Child_Class::output
MyClass::output
Array
(
    [key1] => 1
    [key2] => 2
    [key3] => 3
    [key4] => 4
)
Array
(
    [key1] => a
    [key2] => b
    [key3] => c
    [key4] => d
)

see also: 也可以看看:

My problem was a type, instead of $this->data I wrote $this->$data , the $ sign is only needed at the beginning of each statement. 我的问题是类型,而不是我写$this->data$this->data $this->$data ,只需要在每个语句的开始使用$符号。

My second problem was omitting the $this-> part when accessing variables on parent class thus creating locally scoped variables for parent class which was not shared with child classes, should have used $this->variable . 我的第二个问题是在访问父类上的变量时忽略了$this->部分,因此为父类创建了本地范围的变量,该子类不与子类共享,应该使用$this->variable

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

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