Reply to Re: overwrite private class members in php5?

Your name:

Reply:


Posted by Norbert Wenzel on 08/05/05 10:41

Matthew Weier O'Phinney wrote:
> * Norbert Wenzel <mail@brain4art.at>:
>
>>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
>
>
> $var is empty, so this is setting a non-existent class property to 2.
>
>
>>$object->printVar(); // prints 2
>
>
> I got 1 when running this -- just as I would expect. Are you sure it
> printed 2?
>
> Basically, if a property is undeclared, it is assumed public, so you can
> set undefined properties without issue. If defined private or protected,
> the calling script will not be able to alter the value.
>

i did write the script out of my mind, and didn't paste it. not the best
idea, i know.
thanks for your help, i see my mistakes very clearly. forgive a noob for
such question.

thanks,
norbert

[Back to original 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

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