Req #65843 [Opn->Sus]: Impossibility to assign an object to a property upon declaration

From: Date: Mon, 27 Jan 2020 14:15:45 +0000
Subject: Req #65843 [Opn->Sus]: Impossibility to assign an object to a property upon declaration
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225150@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65843&edit=1 ID: 65843 Updated by: cmb@php.net Reported by: funnyoz at hotmail dot fr Summary: Impossibility to assign an object to a property upon declaration -Status: Open +Status: Suspended Type: Feature/Change Request Package: Class/Object related Operating System: Ubuntu 13.04 PHP Version: 5.5.4 Block user comment: N Private report: N New Comment: This feature request has obvious drawbacks, and is certainly controversial; as such it would need the RFC process. Feel free to start it[1]; for the time being, I'm suspending this ticket. [1] <https://wiki.php.net/rfc/howto> Previous Comments: ------------------------------------------------------------------------ [2013-10-11 09:49:39] worldoffame at hotmail dot com How about making this an option for certain classes such as ArrayObject and SplFixedArray? ------------------------------------------------------------------------ [2013-10-06 14:04:18] rasmus@php.net This would force the definition of this class into the executor losing all the optimizations that go along with being able to fully define classes at compile-time. The performance impact of this would be significant so I don't see this happening anytime soon, if ever. ------------------------------------------------------------------------ [2013-10-06 10:21:09] funnyoz at hotmail dot fr 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 this bug report at https://bugs.php.net/bug.php?id=65843&edit=1

« previous php.bugs (#225150) next »