Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 get properties方法_Php_Oop - Fatal编程技术网

超类中的PHP get properties方法

超类中的PHP get properties方法,php,oop,Php,Oop,我希望我的类从它的超类继承一个getProperties方法 class Mother { function getProperties(){ return get_class_vars(get_class($this)); } } class Child extends Mother{ private $one = 1; private $two = 2; } 问题是,若我在Child上调用getProperties,我会得到一个空结果: $c = new Ch

我希望我的类从它的超类继承一个getProperties方法

class Mother {
  function getProperties(){
    return get_class_vars(get_class($this));
  } 
}

class Child extends Mother{
  private $one = 1;
  private $two = 2;
}
问题是,若我在Child上调用getProperties,我会得到一个空结果:

 $c = new Child();
 var_dump( $c->getProperties() );
返回
数组(0){}

如果我用同一个命令覆盖Child中的getProperties方法,它将按预期工作并返回
array(2){[“one”]=>int(1)[“two”]=>int(2)}
。因此我认为,
$this
解析为母类,而不是继承该方法的类。 如何让Child以我需要的方式继承该方法?或者我如何将
$this
的范围更改为与孩子而不是母亲一起工作?
也许我只是错过了一个非常简单的事实,所以非常感谢您的帮助。

问题是您的属性是
私有的。这使得它们只能由声明类访问,并且只能由声明类访问,而不能由其他类访问。使它们受到保护


请注意,尽管如此,不同的PHP版本之间似乎存在一些不一致之处:

正在编写相同的内容;)+1啊,好的,这很有效,谢谢。我认为该方法和属性将在同一范围内,因此可以看到彼此。@ximarin不,
private
实际上意味着这个类的私有性。:)