php manual - problem with code snippet

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

php manual - problem with code snippet

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
Post Reply