php manual - problem with code snippet
Posted: Mon Oct 31, 2005 3:15 am
Hi,
I am trying out some code from the php manual and....it does not work. Which in mine opinion is quite normal but not according to tha manual
Using
php -v
PHP 5.0.4 (cli) (built: Jun 27 2005 09:53:02)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies
Apache Version Apache/1.3.33 (Unix) PHP/5.0.4
http://www.php.net/manual/en/language.o ... oading.php
this example is from the manual....and it produces
Fatal error: Call to private method Setter::__set() from context '' in /storage/www/test/overloading.php on line 55
which I thing is correct.
Would love to here your opinion about this. 10x.
I am trying out some code from the php manual and....it does not work. Which in mine opinion is quite normal but not according to tha manual
Using
php -v
PHP 5.0.4 (cli) (built: Jun 27 2005 09:53:02)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies
Apache Version Apache/1.3.33 (Unix) PHP/5.0.4
http://www.php.net/manual/en/language.o ... oading.php
Code: Select all
<?php
/**
* Testing overloading features....look php manual
* http://www.php.net/manual/en/language.o ... oading.php
*
*/
class Setter
{
public $n;
private $x = array("a" => 1, "b" => 2, "c" => 3);
private function __get($nm)
{
echo "Getting [$nm]\n";
if (isset($this->x[$nm])) {
$r = $this->x[$nm];
print "Returning: $r\n";
return $r;
} else {
echo "Nothing!\n";
}
}
private function __set($nm, $val)
{
echo "Setting [$nm] to $val\n";
if (isset($this->x[$nm])) {
$this->x[$nm] = $val;
echo "OK!\n";
} else {
echo "Not OK!\n";
}
}
private function __isset($nm)
{
echo "Checking if $nm is set\n";
return isset($this->x[$nm]);
}
private function __unset($nm)
{
echo "Unsetting $nm\n";
unset($this->x[$nm]);
}
}
$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump(isset($foo->a)); //true
unset($foo->a);
var_dump(isset($foo->a)); //false
// this doesn't pass through the __isset() method
// because 'n' is a public property
var_dump(isset($foo->n));
var_dump($foo);
?>Fatal error: Call to private method Setter::__set() from context '' in /storage/www/test/overloading.php on line 55
which I thing is correct.
Would love to here your opinion about this. 10x.