|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-09-20 02:35 UTC] 421034509 at qq dot com
Description:
------------
<?php
class Example{
private $p1;
private $p2;
function __construct($a){
$this->p1=$a;
}
function __get($elmname){
echo "Call_get()!"; //In order to follow the method __get()
return $this->$elmname;
}
function __isset($name){
return isset($this->$name);
}
function __unset($name){
unset($this->$name);
}
}
$example=new Example("v1","v2");
echo $example->p1."<br/>";
var_dump(isset($example->p2));//p2 is uninitialized,the result is bool(false)
echo $example->p2;//This time,__get is called by 1 time.
unset($example->p1);
var_dump(isset($example->p1));//Because of __unset,the result is bool(false)
echo $example->p1;//Amazing!I call the __get just once,but it seems that the __get method has bean called two times!
?>
Expected result:
----------------
Call_get()! v1
bool(false) Call_get()!bool(false) Call_get()!
Actual result:
--------------
Call_get()! v1
bool(false) Call_get()!bool(false) Call_get()!Call_get()!
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 21:00:01 2025 UTC |
see blow, and I think you will find out the reason: <?php class Example{ private $p1; function __construct($a){ $this->p1=$a; } function __get($elmname){ echo "Call_get()!"; //In order to follow the method __get() } function __unset($name){ unset($this->$name); } } $example = new Example("v1"); unset($example->p1); echo $example->p1;