使用父方法时返回的PHP父::$property而不是self:$property
我试图创建一个具有属性/设置数组的抽象类,然后在子类中向该属性/数组添加其他属性。我希望抽象类中定义的方法在从子类调用方法时使用子类属性/数组。我认为下面的代码应该有用。。。但它似乎仍在从父类访问属性使用父方法时返回的PHP父::$property而不是self:$property,php,class,abstract-class,parent,self,Php,Class,Abstract Class,Parent,Self,我试图创建一个具有属性/设置数组的抽象类,然后在子类中向该属性/数组添加其他属性。我希望抽象类中定义的方法在从子类调用方法时使用子类属性/数组。我认为下面的代码应该有用。。。但它似乎仍在从父类访问属性 abstract class AbstractClass { protected static $myProp; public function __construct() { self::$myProp = array( 'a' =&g
abstract class AbstractClass {
protected static $myProp;
public function __construct() {
self::$myProp = array(
'a' => 10,
'b' => 20
);
}
protected function my_print() {
print_r( self::$myProp );
}
}
class ClassA extends AbstractClass {
protected static $myProp;
public function __construct() {
parent::__construct();
self::$myProp = array_merge( parent::$myProp,
array(
'c' => 30,
'd' => 40
)
);
$this->my_print( self::$myProp );
}
}
$myObj = new ClassA;
这应该返回数组([a]=>10[b]=>20[c]=>30[d]=>40)
相反,它返回数组([a]=>10[b]=>20)
我怎么才能让它工作 像这样:
<?php
abstract class AbstractClass {
protected static $myProp;
public function __construct() {
self::$myProp = array(
'a' => 10,
'b' => 20
);
}
protected function my_print() {
print_r( self::$myProp );
}
}
class ClassA extends AbstractClass {
public function __construct() {
parent::__construct();
self::$myProp = array_merge( parent::$myProp,
array(
'c' => 30,
'd' => 40
)
);
$this->my_print( self::$myProp );
}
}
$myObj = new ClassA;
像这样:
<?php
abstract class AbstractClass {
protected static $myProp;
public function __construct() {
self::$myProp = array(
'a' => 10,
'b' => 20
);
}
protected function my_print() {
print_r( self::$myProp );
}
}
class ClassA extends AbstractClass {
public function __construct() {
parent::__construct();
self::$myProp = array_merge( parent::$myProp,
array(
'c' => 30,
'd' => 40
)
);
$this->my_print( self::$myProp );
}
}
$myObj = new ClassA;
实际上,在父类中,您没有定义方法my_print()
来包含任何参数,另外,该方法使用self:$myProp
(非静态:
)。
另外,正如Ka_lin已经回答的,您不需要重新声明在父类中声明的属性
abstract class AbstractClass {
protected static $myProp;
public function __construct() {
self::$myProp = array(
'a' => 10,
'b' => 20
);
}
protected function my_print() {
print_r( self::$myProp );
}
}
class ClassA extends AbstractClass {
protected static $myProp;
public function __construct() {
parent::__construct();
self::$myProp = array_merge( parent::$myProp,
array(
'c' => 30,
'd' => 40
)
);
$this->my_print( self::$myProp );
}
}
$myObj = new ClassA;
如果您这样做(出于某种原因需要重新声明它,就像将预定义值设置为不同于父项一样),您可以做两件事:
首先,您可以将my_print()
更改为接受参数,然后print_r()
参数notself::$myProp
:
protected function my_print($debug) {
print_r($debug);
}
或者,(从5.3.0版起,在PHP中)您可以使用static::
而不是self::
:
protected function my_print() {
print_r(static::$myProp);
}
我会选择第二种选择。
另外,您还应该在php手册中准备更多有关的内容,实际上在父类中,您没有定义方法my_print()
来包含任何参数,另外,该方法使用self:$myProp
(而不是静态:
)。
另外,正如Ka_lin已经回答的,您不需要重新声明在父类中声明的属性
abstract class AbstractClass {
protected static $myProp;
public function __construct() {
self::$myProp = array(
'a' => 10,
'b' => 20
);
}
protected function my_print() {
print_r( self::$myProp );
}
}
class ClassA extends AbstractClass {
protected static $myProp;
public function __construct() {
parent::__construct();
self::$myProp = array_merge( parent::$myProp,
array(
'c' => 30,
'd' => 40
)
);
$this->my_print( self::$myProp );
}
}
$myObj = new ClassA;
如果您这样做(出于某种原因需要重新声明它,就像将预定义值设置为不同于父项一样),您可以做两件事:
首先,您可以将my_print()
更改为接受参数,然后print_r()
参数notself::$myProp
:
protected function my_print($debug) {
print_r($debug);
}
或者,(从5.3.0版起,在PHP中)您可以使用static::
而不是self::
:
protected function my_print() {
print_r(static::$myProp);
}
我会选择第二种选择。
此外,您还应该在php手册中准备更多关于差异的信息,简而言之:您不需要在子类中再次声明属性。谢谢!明白了!你能再详细解释一下发生了什么事吗。如果我重新声明该属性,我会认为它会以null开头(如果有的话)。简言之,区别在于:您不需要在子类中再次声明该属性。谢谢!明白了!你能再详细解释一下发生了什么事吗。如果我重新声明该属性,我会认为它会以null开头。