You are here: Re: [PHP] overwrite private class members in php5? « PHP « IT news, forums, messages
Re: [PHP] overwrite private class members in php5?

Posted by Rasmus Lerdorf on 07/29/05 13:27

Norbert Wenzel wrote:
> Hi, I've done something like this:
>
> class MyClass {
>
> private $var;
>
> function __construct($value) {
> $this->var = $value;
> }
>
> public function printVar() {
> echo($this->var);
> }
>
> }
>
> $object = new MyClass('1');
> $object->printVar(); // prints 1
> $object->var = 3; // Fatal Error as expected
> $object->$var = 2; // no error msg
> $object->printVar(); // prints 2
>
> Hows that possible or what's the difference between
> $object->var and
> $object->$var ?

$object->$var makes no sense. Unless $var is set to 'var' in which case
it is the same thing and you will get the same fatal error as on the
line before. The only way for $object->$var to print 2 in your example
is if you have $var = 'foo' and MyClass has a public property named foo
with a value of 2.

-Rasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация