From: funnyoz at hotmail dot fr Operating system: Ubuntu 13.04 PHP version: 5.5.4 Package: Class/Object related Bug Type: Feature/Change Request Bug description:Impossibility to assign an object to a property upon declaration
Description: ------------ Hello, This is a very important feature in OOP style, upon declaration, one might need to assign an object as a default value, this is currently possible for arrays or primitive types in PHP, but not for objects. Example : <?php class Student { private $address = AddressFactory::newInstance()->createAddress(); } ?> The above code respects the best practices of OOP, it even uses the Factory design pattern, yet in PHP it will fail because it can't be evaluated at compilation. Another simple example, say I've created a wrapper class for arrays, in order to organize the different array functions, the following thing will happen <?php class MyClass { private $array1 = array(); //success private $array2 = new MyArrayClass(); //failure } ?> Instantiating a property in the constructor is not a good practice in object oriented programming : <?php class My Class { private $array; public function __construct(MyArrayClass $array) { $this->array = $array; //I no longer have an empty constructor, my constructor now have //parameters, not OOP recommended. //Many scripts using Reflection API may instantiate // this class without calling the constructor, hence having a null $array property. } } ?> This feature is a must in the next generation of PHP. Just think about it. Test script: --------------- <?php class My Class { private $array = MyArrayClassFactory::createArray(); //hard fail public function __construct(MyArrayClass $array) { $this->array = $array; //I no longer have an empty constructor, my constructor now have //parameters, not OOP recommended. //Many scripts using Reflection API may instantiate // this class without calling the constructor, hence having a null $array property. } } ?> Expected result: ---------------- Compiler's response : success :-) Actual result: -------------- Compiler's response : WTF did you type in line 20 column 12?? -- Edit bug report at https://bugs.php.net/bug.php?id=65843&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=65843&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=65843&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=65843&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=65843&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=65843&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=65843&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=65843&r=needscript Try newer version: https://bugs.php.net/fix.php?id=65843&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=65843&r=support Expected behavior: https://bugs.php.net/fix.php?id=65843&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=65843&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=65843&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=65843&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65843&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=65843&r=dst IIS Stability: https://bugs.php.net/fix.php?id=65843&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=65843&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=65843&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=65843&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=65843&r=mysqlcfg