Page 1 of 1

php manual - problem with code snippet

Posted: Mon Oct 31, 2005 3:15 am
by jmut
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

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);
?>
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.

Posted: Mon Oct 31, 2005 4:19 am
by Chris Corbyn
you're quite right, that is an error in the manual.

Those overloaded methods, should not be private if they are to work correctly in that way.

If you remove the private keyword from the methods (and optionally add "public") then you should get the result they show ;)

Posted: Mon Oct 31, 2005 8:34 am
by feyd
Since you are running 5.0.4, __isset and __unset will not be run as they were/are added in 5.1.0

Posted: Mon Oct 31, 2005 11:40 am
by jmut
feyd wrote:Since you are running 5.0.4, __isset and __unset will not be run as they were/are added in 5.1.0
I agree....sry I just coppied the whole example... I know they are not available in 5.0.4
and I know they will be ok with public....

The problem is that I think the idea of these methods is not to be inhereted actually (and thats why the private method...but then...it does not work).
hmm...maybe we should ask the authors :)

Posted: Mon Oct 31, 2005 11:43 am
by Chris Corbyn
jmut wrote:
feyd wrote:Since you are running 5.0.4, __isset and __unset will not be run as they were/are added in 5.1.0
I agree....sry I just coppied the whole example... I know there are not available in 5.0.4
and I know they will be ok with public....

The problem is that I think the idea of these method is not to be inhereted actually (and thats why the private method...but then...it does not work).
hmm...maybe we should ask the authors :)
They are reserved names though, and I don't belive they will behave like standard public methods if you tried to play with them (which I havent so I cant say for sure).