PHP - 在 child 和 parent 之间传递变量 class
PHP - pass variable between child and parent class
我想知道在 parent 和 child class 之间传递变量的最佳方式是什么,并一直更新这个 class class被执行。
例如我有这个 parent class 里面执行的是 child class:
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();
目前我在parentclass里面执行childclass因为我需要填充parent[=27=的主要数据].这个class会根据一些变量执行childclass。
从 parent 到 child 和从 child 到 parent 继承和分配变量的正确方法是什么?如果我使用依赖注入来检索扩展 class 中的数据,我如何将变量分配给 parent class?
"Do you have an example" - 给你....
<?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();
打印
Child_Class::output
MyClass::output
Array
(
[key1] => 1
[key2] => 2
[key3] => 3
[key4] => 4
)
Array
(
[key1] => a
[key2] => b
[key3] => c
[key4] => d
)
另见:
- https://en.wikipedia.org/wiki/Method_overriding
- http://php.net/manual/en/keyword.parent.php
- What is polymorphism, what is it for, and how is it used?
- https://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29
- https://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29
我的问题是一个类型,而不是 $this->data
我写 $this->$data
,$ 符号只需要在每个语句的开头。
我的第二个问题是在访问父 class 上的变量时省略了 $this->
部分,从而为父 class 创建了局部范围的变量,该变量未与子 class 共享es,应该用$this->variable
.
我想知道在 parent 和 child class 之间传递变量的最佳方式是什么,并一直更新这个 class class被执行。 例如我有这个 parent class 里面执行的是 child class:
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();
目前我在parentclass里面执行childclass因为我需要填充parent[=27=的主要数据].这个class会根据一些变量执行childclass。
从 parent 到 child 和从 child 到 parent 继承和分配变量的正确方法是什么?如果我使用依赖注入来检索扩展 class 中的数据,我如何将变量分配给 parent class?
"Do you have an example" - 给你....
<?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();
打印
Child_Class::output
MyClass::output
Array
(
[key1] => 1
[key2] => 2
[key3] => 3
[key4] => 4
)
Array
(
[key1] => a
[key2] => b
[key3] => c
[key4] => d
)
另见:
- https://en.wikipedia.org/wiki/Method_overriding
- http://php.net/manual/en/keyword.parent.php
- What is polymorphism, what is it for, and how is it used?
- https://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29
- https://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29
我的问题是一个类型,而不是 $this->data
我写 $this->$data
,$ 符号只需要在每个语句的开头。
我的第二个问题是在访问父 class 上的变量时省略了 $this->
部分,从而为父 class 创建了局部范围的变量,该变量未与子 class 共享es,应该用$this->variable
.