| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             [2013-10-06 14:04 UTC] rasmus@php.net
  [2013-10-11 09:49 UTC] worldoffame at hotmail dot com
  [2020-01-27 14:15 UTC] cmb@php.net
 
-Status: Open
+Status: Suspended
  [2020-01-27 14:15 UTC] cmb@php.net
  | 
    |||||||||||||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 16:00:01 2025 UTC | 
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??