Edit report at https://bugs.php.net/bug.php?id=65843&edit=1

 ID:                 65843
 Updated by:         ras...@php.net
 Reported by:        funnyoz at hotmail dot fr
 Summary:            Impossibility to assign an object to a property upon
                     declaration
 Status:             Open
 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 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.


Previous Comments:
------------------------------------------------------------------------
[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

Reply via email to