1--TEST-- 2Test script to verify that magic methods should be called only once when accessing an unset property. 3--CREDITS-- 4Marco Pivetta <ocramius@gmail.com> 5--FILE-- 6<?php 7class Test { 8 public $publicProperty; 9 protected $protectedProperty; 10 private $privateProperty; 11 12 public function __construct() { 13 unset( 14 $this->publicProperty, 15 $this->protectedProperty, 16 $this->privateProperty 17 ); 18 } 19 20 function __get($name) { 21 echo '__get ' . $name . "\n"; 22 return $this->$name; 23 } 24 25 function __set($name, $value) { 26 echo '__set ' . $name . "\n"; 27 $this->$name = $value; 28 } 29 30 function __isset($name) { 31 echo '__isset ' . $name . "\n"; 32 return isset($this->$name); 33 } 34} 35 36$test = new Test(); 37 38$test->nonExisting; 39$test->publicProperty; 40$test->protectedProperty; 41$test->privateProperty; 42isset($test->nonExisting); 43isset($test->publicProperty); 44isset($test->protectedProperty); 45isset($test->privateProperty); 46$test->nonExisting = 'value'; 47$test->publicProperty = 'value'; 48$test->protectedProperty = 'value'; 49$test->privateProperty = 'value'; 50 51?> 52--EXPECTF-- 53__get nonExisting 54 55Warning: Undefined property: Test::$nonExisting in %s on line %d 56__get publicProperty 57 58Warning: Undefined property: Test::$publicProperty in %s on line %d 59__get protectedProperty 60 61Warning: Undefined property: Test::$protectedProperty in %s on line %d 62__get privateProperty 63 64Warning: Undefined property: Test::$privateProperty in %s on line %d 65__isset nonExisting 66__isset publicProperty 67__isset protectedProperty 68__isset privateProperty 69__set nonExisting 70__set publicProperty 71__set protectedProperty 72__set privateProperty 73