Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP education

  • This topic is empty.
Viewing 13 posts - 16 through 28 (of 28 total)
  • Author
    Posts
  • #178742
    __
    Participant

    Seeing this mostly happens with people that only used OOP within PHP, I’d say it is because PHP is a bad teacher

    PHP has always been designed to “keep going” at all costs. Has this led to lazy coding? yes. Has this produced a huge community of “expert beginner” (maybe even a new category of “expert casual”) coders? yes. Does this mean you can’t learn good coding practices in PHP? certainly not. It will make it harder, because there are fewer (qualified) mentors. But the language can handle good coding practices — even nurture them.

    For example, I will freely admit that I don’t know of any other PHP programmers who write interfaces. I’ve never even seen a practical example from PHP’s userland. But PHP is where I learned about interfaces and how to use them. Now, it’s odd to code without (at least a mental) interface.

    It’s not PHP that is a bad teacher, it’s the internet-full of dipsticks who decide to write PHP tutorials because they discovered the magic of how to include one part of their webpage in another. I agree with you that this is a shame, but it’s “how it is”; I’ve had to learn to not let it detract from the language itself. After all, there are plenty of valid complaints about PHP (error handling, nontransitive comparison).

    I also do not disagree that PHP, and its OOP in particular, has a horrendous past.

    before you start learning OOP, make sure you know how functions work.

    definitely.

    #178772
    Alen
    Participant

    in OOP/PHP there is absolutely no need to use inheritance

    Let’s say our application needs a implementation of fetching resources from Flickr. So we have this controller.

    
    class PhotoController {
      
      private $repo;
    
      function __construct(PhotoRepository $repo)
      {
            $this->repo = $repo;
      }
    
      // ...
      
    }
    
    // Contract that every implementation has to offer
    interface PhotoRepository {
      function findById($id);
      function all();
    }
    
    // Our Flickr implementation
    class Flickr implements PhotoRepository {
       public function findById($id)
       {
            // code to find resource
       }
    
      public function all()
     {
         // code to return all results  
     }
    
    }
    

    Currently this is all and good, but there is a high chance you’ll have other resources (Comments, Posts, Schedule, Etc.) to fetch by id or by returning all results. So would it be nice to use inheritance

    In this case you would create a base class you can extend. So something like:

    
    abstract class Base {
       public function findById($id)
       {
          $this->model->find($id);
       }
       public function all()
       {
           $this->model-all();
       }
    }
    
    class Flickr extends Base implements PhotoRepository {
       private $model;
       // inject the model
       function __construct(ModelName $model)
       {
           $this->model = $model;
       }
    }
    

    This way our Base class is dynamic and results it returns depend on what model we inject into the constructor.

    The benefit of using the interface in this case is that if in the future Flickr closes it’s doors and you need to switch to 500px.com implementation then creating a new FiveHundredPX clas that extends our Base and implements the contract. This way your application receives all the necessary functionality that it requires.

    The example is contrived but hey I don’t have all day here :)

    #178785
    chrisburton
    Participant

    before you start learning OOP, make sure you know how functions work.

    Thanks.

    #178824
    __
    Participant

    you’d need to fight [sic] a good mentor, which is not an easy feat. You certainly can’t rely on most guides on the internet

    no, agreed. Deciding which advice you should follow is easily PHP’s greatest weakness.

    in languages like java you can do stuff with inheritance that has no workaround (as far as I now at least).

    curious —like what?

    #178828
    chrisburton
    Participant

    Just to be safe, where’s a great resource to learn functions? I’m looking for something that really takes time to explain it rather than a few tiny paragraphs which is found on PHP.net.

    Also, are there any sites that give you small projects related to functions? Sort of like how Lynda.com does it? Essentially teaching you how something works and then gives you a lesson that you have to code.

    Edit: I decided to give Codecademy another try and so far I’ve learned more about Switchstatements (although I don’t know if I find them useful, just cleaner than a bunch of if/elseif/else statements). I’m at the array part right now and then it goes on to functions. This may just do it.

    #178831
    Alen
    Participant

    Chris, you’re over thinking this, a function is just a reusable piece of code.

    For example…

    function multi($a, $b) {
        return $a * $b;
    }
    

    This abstracts you from writing:

    
    $amount = 5 * 4;
    $diff_amount = 6 * 5;
    

    Instead you would

    
    $amount = multi(5,4);
    $diff_amount = multi(6,5);
    

    This way if fr some reason you need to refactor the way function behaves you only have to do it in one place, function definition, not trying to hunt down all the values you been trying to multiply.

    See this: https://gist.github.com/dwayne/5078372 also try out http://code.tutsplus.com/courses/php-fundamentals

    #178868
    chrisburton
    Participant

    @AlenAbdula You’re right, I was overthinking it. Thanks for the example!

    #178895
    Alen
    Participant

    if you have a very long function

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

    #178911
    __
    Participant

    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.

    #178924
    __
    Participant

    Ah, I see.

    #178960
    chrisburton
    Participant

    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.

    #178973
    Alen
    Participant

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

    #178995
    __
    Participant

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

Viewing 13 posts - 16 through 28 (of 28 total)
  • The forum ‘Back End’ is closed to new topics and replies.