Grow your CSS skills. Land your dream job.

PHP education

  • # August 11, 2014 at 10:00 pm

    So a class is basically describing the purpose?

    By the way, I just started reading that thing and so far it’s great.

    # August 11, 2014 at 10:15 pm

    See this answer at SO.

    http://stackoverflow.com/a/2206835

    # August 11, 2014 at 10:34 pm

    @AlenAbdula Awesome analogy to explain classes! Totally makes sense.

    __
    # August 11, 2014 at 11:39 pm

    We had a private conversation in which he relayed to make my own experiences rather than listen to what other people object to.

    Well, yes, but things are also designed and built with specific purposes in mind. Using a class as a “name” wrapper for a bunch of loosely-related methods might work —in the sense that it accomplishes that goal and doesn’t really break anything in doing so— but there is no advantage in using a class this way.

    # August 12, 2014 at 2:50 am

    Well, I’d argue that that —like most of the language— is an issue of how people use it, not how it actually works.

    Seeing this mostly happens with people that only used OOP within PHP, I’d say it is because PHP is a bad teacher. E.G.: in OOP/PHP there is absolutely no need to use inheritance (afaik but I’m using it anyway), except maybe for reducing the copy pasting of code. Result: people wont learn to use inheritance. This is a shame because it obviously has it’s benefits to use those, such as adding more structure to your code. Another example is the use of interfaces or abstract classes/methods.

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

    __
    # August 12, 2014 at 9:42 am

    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.

    # August 12, 2014 at 1:19 pm

    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 :)

    # August 12, 2014 at 3:15 pm

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

    Thanks.

    # August 12, 2014 at 4:45 pm

    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.

    Even assuming you’re right about the issue being the community and not the language: you’d need to fight a good mentor, which is not an easy feat. You certainly can’t rely on most guides on the internet as you’ve pointed out. OOP is already hard enough without having to wonder if you’re doing it the right way.

    @Alen most of the inheritance situations in PHP can be circumvented with massive copy pasting, while in languages like java you can do stuff with inheritance that has no workaround (as far as I now at least).

    __
    # August 12, 2014 at 4:52 pm

    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?

    # August 12, 2014 at 5:27 pm

    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.

    # August 12, 2014 at 6:34 pm

    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

    # August 13, 2014 at 2:33 am

    curious —like what?
    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 know you could also let it return an “Object”, but that is because every Class is a subclass of Object by default.

    PHP on the other hand is dynamically typed, and therefor has no need for you to specify what this method is supposed to spit out.

    The post above pretty much nailed functions. He forgot one advantage: if you have a very long function, you’ll still only need 1 line to call it instead of copy pasting it everywhere.

    # August 13, 2014 at 5:02 am

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

    # August 13, 2014 at 8:36 am

    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.

Viewing 15 posts - 16 through 30 (of 39 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".