Principles Of Object Oriented Programming in PHP

By Marc Plotz

Object Oriented Programming, or OOP, refers to the method of programming that invokes the use of classes to organize the data and structure of an application. With PHP, OOP started to become feasible with the release of PHP 4, but really came into its own with PHP 5. Now, as the world awaits the release of PHP 6 we await another great leap in the evolution of PHP OOP.

While a large number of programmers shun the use of OOP as a programming paradigm, many more are seeing the advantages of OOP every day. Objects are here to stay - and if you don't like them yet, give it a chance: there comes a time in every developers career when he makes the evolutionary step up to OOP.

In this article we are going explore exactly what OOP is in relation to PHP, and look at a few things you should remember about it as well. We'll end of with a simple example of how to use it.

Objects

The first thing that we need to wrap our heads around is the principle of Classes. Put simply, a Class is an Object, and an Object is a Class. But, like an everyday object, PHP Objects have certain properties, and they can do things. A simple class might look like this:


<?php

class human {
public $gender='Male';
}

The very basic use - and not very good use, I might add - of this simple class would be to get the gender of a human. Unfortunately, all humans instantiated below will be Male by default.

We use the class as follows:


<?php
$Johnny = new human;

echo 'Johnny is a '. $Johnny->gender.'.';

Which will return


Johnny is a male.
               

For simple objects, that may be fine, but what if we needed to set the gender of a human as either male or female?

The Constructor

This is where we start to see the power of OOP. Consider the example below:


<?php

class human {
public $gender;

public function __construct($gender)
{
$this->gender = $gender;
}

public function get_gender()
{
return $this->gender;
}
}

Adding the Contructor Method (function) to the class makes things a little more interesting. First of all, we can now instantiate this class like this:


<?php
$Johnny = new human('male');

As you can see, whatever arguments you pass directly into the object on instantiation are passed into the constructor method of the object. What happens now, if you look at our constructor, is that we have assigned the value of the argument ($gender) to a Property of the object, by using $this. Let's explore that a little more.

$this

This may sound a little loopy (no pun intended) because what on Earth is $this? Think of it as a reference to the current object. So if you have an object and you are referencing or assigning a value to $this->gender, then you are referencing or assigning a value to the gender Property of the object you are currently working with.

self::

Now, $this is a good way of referencing properties, but how do we reference Methods? Remember, a Method is a function inside an object, and gives the Object the ability to DO THINGS.

So, in order to call a Method of the Object I am working in, I can use the self:: call, as below:


<?php

class human {
public $gender;

public function __construct($gender)
{
$this->gender = $gender;

echo self::get_gender();
}

public function get_gender()
{
return $this->gender;
}
}

What you will see now is that once the object is instantiated, it sets the value of $gender, and then runs the get_gender() function, which prints the following to the screen:

Male

This makes setting up and running methods in a current class very simple.

Inheritance

So far, things are simple. But now we take thing once step further by introducing inheritance.

Inheritance is just like it sounds - we talk about one object extending another one, and when it does, it inherits the properties and methods of the parent object.

In terms of out human class, lets try the following:


<?php

class human {
public $gender;

public function __construct($gender)
{
$this->gender = $gender;

echo self::get_gender();
}

public function get_gender()
{
return $this->gender;
}
}

class person extends human {
public $name;
public $surname;

public static function set_name($name)
{
$this->name = $name;
}

public static function set_surname($surname)
{
$this->surname = $surname;
}

public static function get_name()
{
return $this->name;
}

public static function get_surname()
{
return $this->surname;
}
}

$Johnny = new person('male');
$Johnny->set_name('Johnny');
$Johnny->set_surname('Williams');

echo $Johnny->get_name().' '.$Johnny->get_surname().' is a '.$this->get_gender();

$Mary = new person('female');
$Mary->set_name('Mary');
$Mary->set_surname('Williams');

echo $Mary->get_name().' '.$Mary->get_surname().' is a '.$this->get_gender();

Accessors

Accessors are instructions that generally give a property of method permission to be accessible in different ways. There are three types:

public: This property or permission is accessible from anywhere in the application.

protected: This property or permission is accessible only from a class that extends the parent class.

private: This property or permission is accessible only within the current class you are working with.

The really neat thing about access is that it protects your code should other developers be working on the same project. If you don't want them to be able to touch something, don't give him access to it.

In conclusion

In this article we have seen the very basics of PHP OOP. We have found that objects are classes that have properties and methods. We have also found that objects have a constructor class that runs when an object is instantiated, and that we can use SELF or $this in an object to reference methods or properties within the class.



Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •