How else would you construct an object?



  • Here's how we do OOP where I work: looking through PHP code of a certain co-worker, I notice a line like this (anonymized of course):

    [code]$object->__construct($data);[/code]

    The $object is a standard PHP DateTime object. I did a search in my editor, and found 13 of these in the app I'm currently working on. For the non-PHP-literate among you, the constructor of a class is called __construct() in PHP.



  • I'm certainly non-PHP literate, but I'm sure this is missing an ObjectConstructorFactory and an ObjectConstructorFactoryFactory


  • SockDev

    @fowkc said:

    I'm certainly non-PHP literate, but I'm sure this is missing an ObjectConstructorFactory and an ObjectConstructorFactoryFactory

    Nah, that's if you're writing Java.



  • @fowkc said:

    I'm certainly non-PHP literate, but I'm sure this is missing an ObjectConstructorFactory and an ObjectConstructorFactoryFactory

    ...with a DateTimeConstructorFactoryFactoryAapter so you can make DateTimeFactory objects.



  • @toon said:

    $object->__construct($data);
     

    What does that code even do? Does it call the constructor as a normal method, and where is it documentet that you can even write code like that?

     



  • @mt@ilovefactory.com said:

    @toon said:

    $object->__construct($data);
     

    What does that code even do? Does it call the constructor as a normal method, and where is it documentet that you can even write code like that?

     

    That's exactly right: the code calls the constructor like a normal method. In PHP, methods like a constructor are like normal methods, except at certain times they're called by the PHP runtime (if that's the correct term). I think they're called "magic methods". So the above, AFAIK, is perfectly legal, strictly speaking. I mean, you're obviously not supposed to do that, or I wouldn't have posted it. But you can if you want to, like my coworker did.



  • @toon said:

    [...] I mean, you're obviously not supposed to do that [...]

    Surely the double "__" should have ring some bells on this "developer" head that MAYBE this method is private and shouldn't be used outside of the class. Now tell me how other languages don't need private

    I would be very worried about the quality of the code this person is writing if he/she doesn't know about this little detail about PHP

    BTW and FYI, I know this because in Python you pretty much do the same (actually, you could call __init__) ... I wouldn't touch PHP with a ten feet pole



  • @ubersoldat said:

    @toon said:

    [...] I mean, you're obviously not supposed to do that [...]

    Surely the double "__" should have ring some bells on this "developer" head that MAYBE this method is private and shouldn't be used outside of the class. Now tell me how other languages don't need private

    I would be very worried about the quality of the code this person is writing if he/she doesn't know about this little detail about PHP

    BTW and FYI, I know this because in Python you pretty much do the same (actually, you could call __init__) ... I wouldn't touch PHP with a ten feet pole

    Since PHP 5, method privacy (as I'll call it because I can't remember the correct term) is a bit better than in Python. It works like in Java or C++. In Python, you can get around privacy by adding a prefix to the method name. The double underscores mean, that the name is meaningful to PHP, and may be called by the interpreter. Not private per se, just "special" (like my coworker:)). That also means that you're not supposed to write methods that start with two underscores; I've seen "objects" in this shop that have method names starting with up to four or five.



  • @toon said:

    $object->__construct($data);
     

    Have they never heard of "new"? Obviously:

    $object = new DateTime($clone_object->__construct($data)->__data);

     



  • @toon said:

    <font face="Lucida Console" size="2">$object->__construct($data);</font>

    For the non-PHP-literate among you, the constructor of a class is called __construct() in PHP.

    As I read that I thought "Is there a typo? Isn't that exactly what he calls there?".
    Maybe for the non-PHP-literate you should add that this is the constructor's name but is not the syntax used to create objects. (I guess somehting like "new whatever()")

    @toon said:

    Filed under: TRWTF: that "self" thing in Python
    I like using Python in toy projects. But this explicit self syntax is one of the most annoying things about Python.
    And the GIL. Why bother with multi-threading at all when you have a GIL.

     



  • Not a PHP person, but using patterns from other languages....

    1) The constructor does NOT allocation memory, it simply performs all of the initialization.
    2) There are elements that can only be initialized within a constructor.

    So the above, may be being used to return an already existing instance back to its initial state. Granted refactoring the type in question to have the constructor call an Init() method may be an option, but it might not for at least two possible reasons....


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.