From: php at tjworld dot net Operating system: Windows 2003 PHP version: 5.0.5 PHP Bug Type: Class/Object related Bug description: __set() and read-only property prevents proper Class inheritence
Description: ------------ When extending a class that has a visible (public or protected) read-only dynamic property by virtue of __set(), the sub-class is prevented from modifying (or "overloading") the property as if it were merely a public consumer of an instance-object of super, rather than an extension of the class definition itself. This shows up in particular in the DOM classes DOMNode and DOMDocument, where it causes: Fatal error: extDOMElement::__construct() [function.--construct]: Cannot write property in... If you extend both classes, and try to create a new derived extDOMNode object using a custom extDOMDocument->createElement(), it is impossible to set any of the new extDOMNode's dynamic properties from within extDOMNode's constructor (especially ownerDocument) because DOMNodes without an owner are forced to be read-only. The extDOMNode class definition should be able to modify the publically visible ownerDocument property. Since real properties accessible from a sub-class can't be private (if they're to be accessible from other objects), it follows that the same rule should apply to dynamic properties. If this were so the dynamic properties made visible by __set() would be inherited as protected or public and this issue wouldn't arise. Reproduce code: --------------- class ReadOnly { protected $realProperty; private $dynamicProperty = array(); function __construct() { $realProperty = 12; $this->members['test'] = 'read-only'; } public function __set($name, $value) { if($name=='test') { if(isset($this->dynamicProperty[$name])) throw new Exception("Can't overwrite $name"); $props[$name] = $value; } } } class Writeable extends ReadOnly { function __construct($value) { parent::__construct(); $this->realProperty = 25; // ok $this->test = $value; // causes Fatal Error } } $test = new Writeable('write to me'); Expected result: ---------------- The extended class should be able to inherit and modify protected or public properties of the super class. Actual result: -------------- For built-in classes causes a Fatal Error. For user defined classes causes a user-defined read-only result. In the example causes an Exception. -- Edit bug report at http://bugs.php.net/?id=35104&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=35104&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=35104&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=35104&r=trysnapshot51 Fixed in CVS: http://bugs.php.net/fix.php?id=35104&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=35104&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=35104&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=35104&r=needscript Try newer version: http://bugs.php.net/fix.php?id=35104&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=35104&r=support Expected behavior: http://bugs.php.net/fix.php?id=35104&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=35104&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=35104&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=35104&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=35104&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=35104&r=dst IIS Stability: http://bugs.php.net/fix.php?id=35104&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=35104&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=35104&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=35104&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=35104&r=mysqlcfg