[英]Get property call from parent class
I have the following code: 我有以下代码:
class Page extends APage{
/**
* @findby selector
*/
protected $property
public function getPropertyCount(){
count($this->property);
}
}
class APage{
protected function initPropertyByAnnotation(){
}
}
Im using selenium with php, In selenium you select an element with a selector. 我在php中使用了硒,在硒中用选择器选择一个元素。 I want the parent class to detect a call to the childs property so i can handle the actual selecting. 我希望父类检测到对childs属性的调用,以便我可以处理实际的选择。
I thought that this was possible through the __get magic method, but it turns out its only triggered when a property is not defined. 我以为可以通过__get magic方法实现这一点,但事实证明,它只有在未定义属性时才会触发。
Is there a way to detect the call some way without using a helper method like getProperty? 有没有某种方法可以在不使用诸如getProperty之类的辅助方法的情况下检测到呼叫?
You can use the __get()
function changing the scope of your property to private . 您可以使用__get()
函数将属性的范围更改为private 。
As the docs says: 正如文档所说:
__get() is utilized for reading data from inaccessible properties. __get()用于从不可访问的属性读取数据。
public function __get($field) {
if ($field == 'property') { //the name of your property
//do something that you want
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.