php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65843 Impossibility to assign an object to a property upon declaration
Submitted: 2013-10-06 10:21 UTC Modified: 2013-10-06 14:04 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: funnyoz at hotmail dot fr Assigned:
Status: Open Package: Class/Object related
PHP Version: 5.5.4 OS: Ubuntu 13.04
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-10-06 10:21 UTC] 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??

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-06 14:04 UTC] 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-11 09:49 UTC] worldoffame at hotmail dot com
How about making this an option for certain classes such as ArrayObject and SplFixedArray?
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Mar 20 02:01:27 2019 UTC