Re: overwrite private class members in php5?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Norbert Wenzel <mail@xxxxxxxxxxxx>:
> 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.

-- 
Matthew Weier O'Phinney
Zend Certified Engineer
http://weierophinney.net/matthew/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux