从父类获取属性调用


Get property call from parent class

我有以下代码:

class Page extends APage{
    /**
     * @findby selector
     */
    protected $property
    public function getPropertyCount(){
        count($this->property);
    }
}

class APage{
    protected function initPropertyByAnnotation(){
    }      
}

我在php中使用selenium,在selenium中,您可以用选择器选择一个元素。我希望父类检测到对childs属性的调用,这样我就可以处理实际的选择。

我认为通过__getmagic方法可以实现这一点,但事实证明,只有在未定义属性时才会触发它。

有没有一种方法可以在不使用像getProperty这样的辅助方法的情况下以某种方式检测调用?

您可以使用__get()函数将属性的范围更改为private
正如医生所说:

__get()用于从不可访问的属性中读取数据。

public function __get($field) {
    if ($field == 'property') { //the name of your property
        //do something that you want
    }
}