Skip to content
bharathwaj.com

Article

PHP Patterns with more practical examples - Classes

2 min read

PHP classes are the most basic part of Object Oriented Programming. I'd like think about classes as a way of organisaing related actions into a simple template.

If you are new to Object Oriented Programming, i suggest you start with simple classes (template for related actions). Start by looking at the procedural code you have and try to split them into smaller functions that somewhat do similar things and put them in a PHP class.

Lets look at a simple example. For the sake of this article lets take an example of a basic Events system. Lets assume we already have an event created in the backend, all we want to do is, list the events to begin with. We can then expand on this idea further and see how a procedural code can get messy and then clean it up by using a class.

Procedural code for listing Events

We’ll be working with a json file containing the list of events. The json looks something like this.

[
    {
        "title": "The Annual",
        "desc": "The Annual description about this event",
        "date": "01-05-2020",
        "venue": "1 the ranndom place hall"
    },
    {
        "title": "The Expert Conference",
        "desc": "The Expert Conference description about this event",
        "date": "02-04-2020",
        "venue": "2 the ranndom place hall"
    },
    {
        "title": "Scholar’s Conference",
        "desc": "Scholar’s Conference description about this event",
        "date": "03-03-2020",
        "venue": "3 the ranndom place hall"
    },
    {
        "title": "Capstone Conference",
        "desc": "Capstone Conference description about this event",
        "date": "04-02-2020",
        "venue": "4 the ranndom place hall"
    },
    {
        "title": "Interstate Conference",
        "desc": "Interstate Conference description about this event",
        "date": "05-01-2020",
        "venue": "5 the ranndom place hall"
    },
    {
        "title": "Allstar Conference",
        "desc": "Allstar Conference description about this event",
        "date": "06-06-2020",
        "venue": "6 the ranndom place hall"
    }
]

PHP procedural code

In PHP, you would do something like this to display all the events on the page.

// Get events from json
$events = file_get_contents('http://example.com/events.json');

// Convert the json content into an array so we can work with it in PHP
$events = json_decode($events, true);

// Loop through the array and display
$outHtml = "";
foreach ($events as $event) {
    $outHtml .= "Title:" . $event['title'] . "\n";
    $outHtml .= "Description:" . $event['desc'] . "\n";
    $outHtml .= "Date:" . $event['date'] . "\n";
    $outHtml .= "Venue:" . $event['venue'] . "\n\n";
}
// Echo output
echo $outHtml;

Although this is a very simple example, we will be building on this idea to demonstrate patterns and other Object Oriented Principles.

class
{

}

Hope you find this article useful 👍.

Originally published at