note 53539 deleted from language.oop5.overloading by nlopess
| From: | nlopess@php.net | Date: | Fri, 28 Oct 2005 18:11:47 +0000 |
| Subject: | note 53539 deleted from language.oop5.overloading by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-97753@lists.php.net to get a copy of this message | ||
Note Submitter: Dasko <dasko at mp dot pl>
----
Usually, if you try to access private variables from outside your class, fatal error will be
triggered. But when one of the property overloading methods(__set() or __get()) was defined the
class behaves like the requested property wasn't defined at all and overloads it.
Example:
<?php
class MyClass {
private $foo;
public function __get($name) {
echo $name . ' doesn\'t exist...<br />';
}
public function __set($name, $value) {
echo 'Cannot set ' . $name . '. The requested property doesn\'t
exist...<br />';
}
}
// class' instance
$obj = new MyClass;
// trying to access foo - __get() is called
echo $obj->foo;
// trying to set foo - __set() is called
$obj->foo = 'newValue';
?>
The code won't trigger any error. It simply behaves like MyClass::$foo really doesn't
exist.
Notice, that the feature doesn't work with method overloading - accessing private method from
outside the class will cause Fatal Error, even if the __call() method was defined.
The feature was tested on PHP 5.1-dev.