Close

January 14, 2014

Starting OOP in PHP

By doing a quick search in the mighty Google Search for “PHP OOP”, you get a ton of results. There are the good ones and the bad ones as always, and hopefully one day, this one. I will also be covering the real basics of OOP in PHP, for me, to recap, and hopefully for someone to learn from it.

What is OOP?

OOP is Object-Orientated Programming, what this means is that you use objects in your application to do certain tasks and use it to define certain “things” you will be using in your application. In php, to create an object you have to create a class and “instantiate” the class to create an object.

Note: A class in PHP is not an Object. The difference between a class and an object is the following: A Class in PHP can be seen as the “plan” or “blueprint” for your object, which it the “thing” you want to use.

To define a class in PHP is simple:

<?php
class Task
{
    public $name;

    public function printName()
    {
        return "The task name is : " . $this->name;
    }
}

$task = new Task();
$task->name = "SomeTask";
echo $task->printName();

In the code sample above, we create a class with the name Task, this is what the class keyword is for. This is the blueprint for any task going forward. We give it a value holder for the name of the task, which is called a property of the class. We also add a function in the class which will echo out the name of the task, these functions in a class setting are called ‘methods’.

To create a new task object based on the Task class, we use the keyword new, this tells PHP that it needs to create a new object based on the class. After we create the task object, we set the name property of the task object as “SomeTask”, and finally we print out to the screen the value returned by the printName() method.

Tip: A property is something that an object has, like a car has a wheel, a bird has a wing, a mouse has a tail and a shopping cart has a product. A method is something that the object can do, like a car can drive, a bird can fly, a mouse can run and a shopping cart can calculate the total of its contents. If you get that difference you should be good to understand the difference between when something needs to be a property and when it has to be a method.

Visibility

As you can see from our class above, the property and the method have the public prefix to them. This means that the method or the property can be “called” (this is the same as executed) from anywhere in the application. There are 3 levels of visibility, “public”, “protected” and “private”. If you were to change the visibility of the method to private, you will get an error that says the method can only be run from within that class from within other methods. If you should change it to protected, you will also get the error, stating that the method can only be run from within that class or any sub-classes which inherit from it. (We will be looking at inheritance in the next article). I’ll leave you to change and play with the code to see what results you get.

That, in a nutshell is the bare bones basics of how to get started in OOP in PHP, there are a lot of subjects still to cover in OOP in PHP and I’ll cover them as we go along. For now though, that is it and I’ll update this entry with links to the other related articles.

Leave a Reply

Your email address will not be published. Required fields are marked *