[英]Magic Methods php, __call __get and __set?
my question is, say we have a class: 我的问题是,我们有一堂课:
class SomeClass{
private $someProperty;
public function __call($name,$arguments){
echo "Hello World";
}
Now when I say: 现在当我说:
$object = new SomeClass();
$object->someMethod();
the __call method in my class will be called. 我的类中的__call方法将被调用。
When I say 当我说
$object->getSomeProperty();
will __call again be called? 将__call再次被调用? If so, what is __get and __set magic methods are for?
如果是这样,那么__get和__set魔术方法是什么用的?
When I say 当我说
$object->someProperty;
then will __get($someProperty) be called? 然后会调用__get($ someProperty)? or will it be __set($someProperty) ?
或者它会__set($ someProperty)?
Anytime an inaccessible method is invoked __call
will be called. 无论何时调用不可访问的方法,都会调用
__call
。
Anytime you try to read a property __get
will be called, whether it's echo $obj->prop;
无论何时尝试读取属性,都会调用
__get
,无论是echo $obj->prop;
or $var = $obj->prop;
或
$var = $obj->prop;
And lastly, anytime you try to write to a property the __set
magic method will be called. 最后,无论何时尝试写入属性,都会调用
__set
魔术方法。
will __call again be called?
将__call再次被调用?
Yes. 是。
If so, what is __get and __set magic methods are for?
如果是这样,那么__get和__set魔术方法是什么用的?
see below: 见下文:
When I say
当我说
$object->someProperty;
then will __get($someProperty) be called?
然后会调用__get($ someProperty)? or will it be __set($someProperty) ?
或者它会__set($ someProperty)?
__get('someProperty')
because this expression is not an assignment. __get('someProperty')
因为这个表达式不是赋值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.