A Simple PHP OOP Tutorial -Learn Object Oriented Programming Concepts with Examples in 7 steps

Demo Download Country Class Example

In this post, I'll explain the php oop concepts as simple as possible.

What is Object Oriented Programming? How to learn php oop easily

Object oriented is not some kind of code, but a different approach to the way of programming.  There are lots of drawbacks in the procedural methodology, which the OOP can overcome. Lets gets straight into the Country-Continent Class example which will guide you to most of the main techniques used in PHP OOP.

Continent -Country Class Example

I've made up two classes , Continent and Country which explains all the major oop concepts. Country inherits from Continent, means Country is the child class of Continent or Continent acts as the parent class to Country.

Step 1. How is a Class  defined?

A Class is defined using the keyword Class, here

Class Continent {

}

  • The curly braces represents the scope of the class, or where the class starts and ends.

Before getting into the code there are two major concepts(terms) which you need to know.

  1. Properties  - Properties are variables itself, but in OOP we call variables as property.
  2. Methods    - Methods are functions itself, but in OOP we call functions as methods.

Class Continent

Step 2. Properties

Class Continent {
// Properties of the Continent Class
var $name;
var $size;
// Assigning a static value to the static variable.
static $smallestContinent="Australia";

These are properties of the Class Continent. Properties are defined using the keyword var. Everything is pretty straight forward, except the scope. There are three scopes.

  1. Private
  2. Protected
  3. Public

Here the  properties 'size' and 'name' are of public scope (which is the default scope).I've explained the scopes in detail below.

The third variable smallestContinent have a prefix static . This means this variable will be having  static value and it cannot be changed, I will explain this also in detail after explaining the concept of objects.

 Step 3. Methods

Class Continent has one method  getDetails of scope public. Likewise properties, functions are also of  3 scopes (public,private,protected).

/*
Getting the details of the country or continent through the public function getDetails.
*/
public function getDetails($type){
// Returning the assigned values to the properties of the class Continent.
$details= $this->name." is a ".$this->size." ".$type;
return $details;
}

Step 4. Real world analogy and concept of Objects

Objects makes the oop possible. We create a new object of the Class and that object holds all the properties and methods of the class. As a real world analogy to our class Continent, Class Continent has lots of properties like  size, name and all and its methods. And sub class Country extends from Continent and its supposed to have all these properties too.

Assume the name property of the Class Country.  Different countries in this whole world have different names. So what we are doing is that we create different objects for the class country and giving  it different names (India,USA,Australia). All the code stands inside the  class and we can use or reuse  them. Assume that there is a class for a user registration system, we can implement this  registration system anywhere in your website by creating a new object for the user registration class.

//Creating an object of Continent class. Constructor will be called automatically.
$continent=new Continent($type);
// Assigning values to Continent class properties
$continent->name="Asia";
$continent->size="Big";
// Calling a public method(function) of the class Continent
echo $continent->getDetails('Continent');

Here  a continent object is created by,

$continent=new Continent($type);

The attributes of the Continent class are assigned specific values.  Values are assigned to the public properties name and size. Also the public method getDetails is called with the object instance.

In the Continent Class,

/*
Getting the details of the country or continent through the public function getDetails
*/
public function getDetails($type){
// Returning the assigned values to the properties of the class Continent.
$details= $this->name." is a ".$this->size." ".$type;
return $details;
}

We are assigning values Asia and Big to  name and size attributes of the continent object respectively  and this values are appended to some content and returned from the class.

Step 5. $this in php oop  

$this is a simple concept in php oop, but it can be really messy if you doesnt know its purpose. I really had problems with $this when I started learning oop.

$this is nothing but just the current object. Here we are creating an object of the Continent class ($continent) , assigning values to the properties and calling the function getDetails. $this->name in the getDetails function now will be having the  value assigned for the continent object's name property.

$continent->name="Asia";

That means  $this->name will be having value "Asia" .

Output of the getDetails function

Asia is a Big Continent

Step 6. Constructor  

Constructor is a special function, which will be called right after the object creation.  Here we've defined a constructor in the Continent Class.

public function __construct($type){

echo "This is the constructer invoked for the $type object";
}

A constructor is created with the keyword __construct or the name of the Class itself. Constructor is valid for the child classes object instances also.

Step 7. Destructor

Destructor is called once there are no more references to the particular object. Destructor  function can be created with the keyword __destruct.

function __destruct() {

echo "Destructor is called once there are no more references to the particular object";
}

 

 

Author: Linjo Joson

Myself Linjo Joson, here in dollarfry , I write articles about creating and optimizing websites as well as making money from your online business. I am PHP/WordPress developer by proffession.I love web analytics and driving traffic to them(doing this for 9 years). You can find me on Google+