PHP Classes
PHP Classes
elePHPant
Icontem

Extending PHP Classes and the Object Model

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Extending PHP Classes...   Post a comment Post a comment   See comments See comments (10)   Trackbacks (0)  

Author:

Posted on:

Categories: PHP Tutorials

Nowadays many PHP developers use Object Oriented Programming (OOP). However not every PHP developer really understands why that is a good thing.

Some use OOP just because they see others using it, without knowing very well its benefits nor how to create a consistent object model that addresses the needs of their applications.

Read this article to learn how objects can represent the real world through classes and how you can create an object model for your PHP application.




Contents

Introduction

Real World Structure

Class Structure

The Elements

How it is Used

Where the Magic Happens


Introduction

This tutorial is targeted to beginning developers with little to no knowledge of objects.

In the world, many things are related to each other in one way or another. In Object Oriented Programming (also known as OOP), one of the fundamental concepts is that objects can also be related to each other. Here is an example of real world transportation related items:

Real World Structure

Transportation
    Ground
        Bicycle
        Tricycle
        Auto
            Truck
            Van
            Car
    Air
        Plane
        Helicopter
        Balloon

Class Structure

We can define all of these real world relationships using classes, like this:

class transportation {
    
    private $thisClass;
    protected $transpoType = 'General';
    protected $fuelType = 'Various';
   
    public function __construct() {
        
        $this->thisClass = get_class($this);
    }

    public function showResult() {
        
        echo 'The current class is '.$this->showClass().'<br>';
        echo 'Transportation type is '.$this->showTranspoType().'<br>';
        echo 'Fueled by '.$this->showFuelType().'<br>';
        
    }
    
    final private function showClass() {
        
        return $this->thisClass;
        
    }
    
    final private function showTranspoType() {
        
        return $this->transpoType;
        
    }

    final private function showFuelType() {
        
        return $this->fuelType;
        
    }
}

class ground extends transportation {
    
    protected $transpoType = 'Ground';
    
}

class air extends transportation {
    
    protected $transpoType = 'Air';
    
}

class bicycle extends ground {
    
    protected $transpoType = 'Bicycle';
    protected $fuelType = 'Manpower';
    
}

class tricycle extends ground {
    
    protected $transpoType = 'Tricycle';
    protected $fuelType = 'Manpower';
    
}

class auto extends ground {
    
    protected $transpoType = 'Auto';
    
}

class truck extends auto {
    
    protected $transpoType = 'Truck';
    protected $fuelType = 'Fossil Fuels';
    
}

class van extends auto {
    
    protected $transpoType = 'Van';
    protected $fuelType = 'Fossil Fuels';
    
}

class car extends auto {
    
    protected $transpoType = 'Car';
    protected $fuelType = 'Fossil Fuels';
    
}

class plane extends air {
    
    protected $transpoType = 'Plane';
    protected $fuelType = 'JP5 Jet Fuel';
    
}

class helicopter extends air {
    
    protected $transpoType = 'Helicopter';
    protected $fuelType = 'JP5 Jet Fuel';
    
}

class balloon extends air {
    
    protected $transpoType = 'Hot Air Balloon';
    protected $fuelType = 'Heated Air';
    
}

The Elements

Lets take a minute and examine what he have done here.

The first class, transportation, is the real work horse in this scenario and we will refer to it as the super class. Every class can contain properties (variables) and methods (functions).

This class defines 3 properties, thisClass, transpoType and fuelType. It also defines 5 methods, __construct, showResult, showClass, showTranspoType and showFuelType. The properties and methods are known as members.

Notice the public, protected and private keywords before each member, these set the visibility. The visibility defines where the member can be used. Public members can be used outside of the class structure, protected members can only be used within the class structure and private members can be used only within this class.

The class structure is this super class and every sub-class (child class) that extends it. One other keyword you will notice here is the keyword final, this defines the member as unchangeable by any of the sub-classes. We will discuss what each method does a little later in this article.

The next class, ground, extends the transportation class. By extending the class, it inherits all of the super classes members and can change the ones visible to it, public and protected members. This class is changing the value of the transpoType property to Ground.

Skip down to the bicycle class which extends the ground class. Since ground has extended transportation, this class also inherits all of the super classes' members and can change the ones visible to it. It is changing the transpoType property to Bicycle and the fuelType property to Manpower.

As you go through the rest of the classes, you can see that we have created the real world relationships.

Lets go back to the super class and take a look at the methods that will be doing the work.

The first method is __construct. This is a magic method that is called whenever a class is instantiated. Since none of the sub-classes are calling a __construct method, this one will always be the one called regardless of the class being instantiated. This method is defining the property thisClass with the name of the class instantiated.

Notice the pseudo-variable $this, whenever a class is instantiated as an object, $this refers to the current object. We will go over $this in a little more detail later.

The next method is showResult and is the only public method we have defined, which means it is the only method we can call from outside the class structure. Its job is to output the results to our screen. It makes calls to the showClass, showTranspoType and showFuelType methods. These methods only have one job and that is to return the value of their respective properties.

How it is Used

You may be wondering why we went to all this trouble building this class structure. The answer is in how we are now able to use it. Refer to the following code:

$bicycle = new bicycle();
$bicycle->showResult();

Here we have instantiated the class bicycle as a new object which we have named $bicycle and then call the method showResult. The output we will get is:

The current class is bicycle
Transportation type is Bicycle
Fueled by Manpower

As you can see, we have the details related to a bicycle. Now let's run this code:

$plane = new plane();
$plane->showResult();

The output we will get is:

The current class is plane
Transportation type is Plane
Fueled by JP5 Jet Fuel

We now have the details related to a plane.

Where the Magic Happens

The real magic is taking place with the use of the pseudo-variable, $this. As mentioned earlier, $this will always refer to the object it belongs to.

$bicycle is an object referencing the bicycle class, which extended the ground class, which extended the transportation class. $plane is an object referencing the plane class, which extended the air class, which extended the transportation class.

Using $this in the super class refers to the object, not the class it resides in, so the results are the properties as defined by the referenced class.

This concept is the heart of the Object Model, a well defined class structure can represent anything we experience in the real world.

Conclusion

Using OOP in your PHP application is a good thing but once you realize why, all becomes much clearer and what you do makes much more sense. You get more confident that you are doing the right thing once you are able to design an object model that addresses your application needs.

Post a coment here if you have questions or other types of comments about this simple introduction to implementing object models in PHP.




You need to be a registered user or login to post a comment

1,512,413 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

7. OOP Explanation For Beginner - Muhammad Junaid Babar (2015-06-02 23:06)
Good explanation of oop with the basic point... - 0 replies
Read the whole comment and replies

6. Good Explaination with example - suman wadhwani (2015-06-01 06:10)
Easy to Learn the Class and object with example... - 0 replies
Read the whole comment and replies

5. Very good post - Thiago Arcanjo (2015-05-25 19:26)
User OOP simplify our development... - 0 replies
Read the whole comment and replies

4. nice job - Ricardo Luz (2015-05-21 03:41)
Very simple and good explanation... - 0 replies
Read the whole comment and replies

3. OOP tutorial - Jim Burns (2015-05-21 01:38)
Nice tutorial... - 2 replies
Read the whole comment and replies

1. OOP Syntax - Jurgen_v_O (2015-05-21 01:38)
OOP Syntax just is not Intuitive to me.... - 1 reply
Read the whole comment and replies

2. Great OOP Example - Gerry Danen (2015-05-20 21:44)
Simple and clear introduction to OOP... - 0 replies
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Extending PHP Classes...   Post a comment Post a comment   See comments See comments (10)   Trackbacks (0)