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: 2020-01-27 14:15 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: Suspended Package: Class/Object related
PHP Version: 5.5.4 OS: Ubuntu 13.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
22 + 45 = ?
Subscribe to this entry?

 
 [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?
 [2020-01-27 14:15 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-01-27 14:15 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 10:01:26 2024 UTC