PHP OOP – Simple Real-World PHP OOP Example

designobject-orientedPHP

I'm trying to learn PHP OOP, but when I've followed tutorials, all the examples seem to involve things like:

class Human {
    $_sex;

    public function setSex($sex) {
        $this->_sex = $sex;
    }

    public function getSex() {
        return $this->sex;
    }
}

(Pointless example, but hopefully you get what I mean — An object has properties that are stored in the object.)

I've never come across a web application where I've needed to create objects about humans, dogs, cars, or any of the other weird examples you find when reading tutorials about OOP…

So I'm trying to find a similarly simple, but more realistic, project with which to learn OOP. I've chosen a website for an internet comic (e.g. XKCD.com). (This would allow people to view the comic, but also the author to edit and update it.)

So how would this translate to real-world PHP OOP?

My initial instinct would be to break the objects down thusly:

  • Catalogue
  • Episode

Where the Catalogue class would contain methods pertaining to all the episodes of the comic, e.g. getMostRecentEpisodeID(), deleteEpisode($episodeID), addNewEpisode($newEpisode), etc. etc.

And Episode would contain methods pertaining to the individual episode, e.g. getEpisodeComments($episodeID) (if the website allowed people to leave comments on individual episodes), editEpisode($episodeID), getEpisode($episodeID), etc.

Is this right, or have I made an absolute hash of what OOP is for?

Edit: Rather than just giving me a list of examples, it would really be helpful if your answer made specific reference to the problem I'm trying to solve.

Thanks.

Best Answer

The problem with Object Oriented Programming is exactly that, the shape and vehicle examples. What I'm going to type below is not the actual truth, it is an opinion. An opinion shared by more people than just me though :)

Object oriented vs "Class oriented"

Your example where there would be a catalog and an episode is perfectly well. The problem is the functions you propose these Objects should get. That is where you go from Object Oriented Programming to Class Oriented Programming. Just fitting a lot of methods concerning the same concept in an Object doesn't make programming object oriented. All you do is make some sort of container for methods.

The Episode Object should be just an Object that "models" the Episode. It has a title, content, author, that kind of stuff. In no way should this Episode have access to the database. It is not supposed to save itself or be able to load more instances of other episodes. The same goes for Catalog, it should not be able to retrieve its newest Episode itself. A catalog is a model, it has a title and some other stuff.

Splitting up responsibilities

Getting stuff from and saving stuff to a DataBase is not a models responsibility. So someone else has to do the job. A good start might be to create a CatalogRepository which could look like so:

CatalogRepository
{
    /**
     * @param int $id
     * @return Catalog
     */
    public function getCatalogById($id)
    {
        //Do a query, populate a new Catalog and return it. 
        //The constructor of Catalog should not be the one quering! 
        //It should receive data, nothing more
    }

    /**
     * @param Catalog $Catalog
     */    
    public function saveCatalog(Catalog $Catalog)
    {
        //stuff....
    }

    /**
     * @param string $name
     * @return Catalog[]
     */    
    public function searchCatalogsByName($name)
    {
        //do a full text search or something?
    }
}

The Episode would have something along the same lines where you could get all Episodes for a specific Catalog but also the five most popular Episodes:

class EpisodeRepository
{
    /**
     * @param Catalog $Catalog
     * @return Episode[]
     */
    public function getEpisodesByCatalog(Catalog $Catalog)
    {

    }

    /**
     * @param int $amount
     * @return Episode[]
     */
    public function getMostPopularEpisodes($amount)
    {

    }
}

There are some really good guidelines for writing good object oriented code, take a look at SOLID for instance

Related Topic