note 53539 added to language.oop5.overloading
| From: | Dasko | Date: | Sun, 05 Jun 2005 11:39:13 +0000 |
| Subject: | note 53539 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89944@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53539
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53539&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53539&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53539&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53539&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53539&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53539&report=yes
Search -- http://master.php.net/manage/user-notes.php