• # August 13, 2014 at 8:57 am

    This reply has been reported for inappropriate content.

    Single responsibility principle also applies to functions. Your function should do one thing, one thing only and do it well.

    Functions can still be quite a couple of lines. Some things are just complicated. I’m not saying 100’s of lines, but if you got to copy paste a function of 20 lines a tenfold that’s a lot of redundant code.

    # August 13, 2014 at 11:25 am

    Like a method that might return 2 (or more) distinct classes. Unfortunately you can only specify one class (and you have to specify one). The solution is to create a superclass and specify this one as the class that the method will return.

    I’m not quite sure I follow. Are you talking about a subclass’ method returning an instance of the superclass? or that, by specifying that a class returns Superclass, you have your pick of returning SubclassA or SubclassB?

    The former case sounds like an implementation of the Factory pattern.

    In the latter, PHP does recognize that child classes inherit from their parent(s) —for example:

    class super{}
    class sub extends super{}
    $sub = new sub;
    $sub instanceof sub;       // true
    $sub instanceof super;     // true
    $sub instanceof stdclass;  // true

    If you’re talking about being able to specify return value types/classes, you’re right: PHP doesn’t do this. But it’s not too difficult for the programmer to do so in documentation. (Again, I acknowledge that most PHP coders don’t; and there are cases where I would have liked to have been able to force them to.)

    If I have missed the point entirely, please elaborate. I don’t work much with Java.

    # August 13, 2014 at 11:41 am

    This reply has been reported for inappropriate content.

    Say I have a method I want to either return class1 or class2. I can’t tell java that this method can return 2 distinct classes. Instead I create a Superclass and define both class1 and class2 to extend/implement this superclass. Now I can safely say public Superclass myMethod(). This forces you to use inheritance, and teaches you it’s principles. Yes, PHP can do the same thing, but you can circumvent it and a lot of people wont learn it because of that.

    # August 13, 2014 at 12:08 pm

    Ah, I see.

    # August 13, 2014 at 12:52 pm

    This reply has been reported for inappropriate content.

    Java was the first language I learnt, and more than anything else it was the fact that the documentation (from Oracle’s beginner tutorials onwards) explicitly discuss and demonstrate the benefits of inheritance, interfaces and OOP in general that makes it a good introduction.

    I know it’s low level stuff, but pages like this are early reading in their tutorials:

    Explaining inheritance:

    Explaining interfaces:

    Demonstrating interfaces:

    I would argue this is useful reading even for PHP beginners. Most of their tutorials work virtually as well for PHP.

    # August 13, 2014 at 9:07 pm

    I’m going to take a step back from this thread. Some of it is already confusing.

    @TheDoc @paulie_d Can someone enable @taxicss‘s comment? When you click on the latest comment for PHP it directs to this post but nothing is showing up.

    # August 13, 2014 at 10:55 pm

    @chrisburton don’t worry about any of this. Just take it slow, it will click.

    # August 13, 2014 at 11:14 pm

    Yeah — sorry for the detour. pay attention to functions for right now.

    # August 14, 2014 at 9:16 am

    This reply has been reported for inappropriate content.

    Focus on function indeed, and once you grasp them find a mentor for OOP (it’s very hard without mentor, in any language. Don’t rely on tutorials alone). Java has a higher chance of you learning it the proper way, but you can do it in PHP.

Viewing 9 posts - 31 through 39 (of 39 total)

You must be logged in to reply to this topic.