Introduction to Object Oriented PHP – Pt. 1

A year of working as a Front-end Magento developer without major PHP experience has been a steep learning curve and despite having learned a great deal, I thought it was about time I got some serious PHP knowledge under my belt.

My Plan: To nail the fundamentals of Object-Oriented PHP.

The End Goal: To get a thorough understanding of what is going on under the hood of PHP frameworks, to help me write better code and debug issues with minimal perspiration.

Whats the point of Object Oriented Programming?

Working with Objects allows us to wrap functionality and properties for a specific entity together. This keeps things separate and allows us to access those properties and functions without fear of anything spilling over on to any innocent bystanders.

To the untrained eye, OOP can seem like an unnecessary effort but it makes code more maintainable and easier to modify, and when applications grow, it can and will save a lot of headaches.

The majority of PHP frameworks will implement an object-oriented approach, so if you want to gain an understanding of whats going on behind the scenes, it is important to gain a thorough understanding of object oriented PHP.

Need to Know?

There are a few OOP-specific terms which you will need to know:

  • Class: What a thing should look like.
  • Object: A thing
  • Instantiate: To create a new thing. (A new object with a given class).
  • Method: A function belonging to a class.
  • Property: A variable belonging to a class.

 

Animal Class

First thing we will do is create a new class. For the sake of this example I will create an ‘Animal’ class.

//animal.php
 
Class Animal {
 
    public $name = ‘’;
 
    public function __construct( $name ) {
 
        $this->name = $name;
 
        return true;
 
    }
}

What we have done here is:

  • Defined a new Animal Class.
  • Said that aAnimal should have a ‘name’ property.
  • Created the method that will run when a new Animal is created.    __construct() is a standard method for all Classes and it explains how to build a new instantiation of that Class. In this case, it takes the value passed in and set it as the new Animals name.

Now that our Animal blueprint exists, lets make a new Animal.

//dog.php
 
$dog = new Animal(‘Rufus’);

This will create a new Animal with the name Rufus which we can access through the $dog reference. e.g. $dog->name.

//dog.php
 
$dog->name;
 
// returns ‘Rufus’.

At the moment Animals are pretty limited. Lets teach them to say hello:

//amphibian.phppublic function movesBy( $travelMethod ) {
 
    echo $this->name . ' gets about by ' . $travelMethod . '.';
 
}


$dog->movesBy( 'running' );
// echoes ‘Rufus gets about by running.’

What is “$this”?! I hear you scream.

To explain the use of $this, let me extend the movesBy method in the above example slightly. Turns out, every Wednesday Rufus becomes a Super Dog and is able to fly.

//dog.php
 
public function movesBy( $travelMethod ) {
 
    $dayofweek=date('w',strtotime($date));
    $today    =date('Y-m-d',strtotime(($day-$dayofweek).' day',strtotime($date)));
 
    if ( $today == 'Wednesday' ) {
        $travelMethod = 'flying';
    }
 
   // Ignore the above code - it used for demonstrative purposes only, there is no need to understand this!
 
    echo 'Today is ‘ . $today . ' so ' . $this->name . ' gets about by ' . $travelMethod . '.';
 
}


$dog->movesBy( )
// echoes ‘Today is Wednesday so Rufus gets about by flying. ’ (assuming today is Wednesday)
// echoes ‘Today is Friday so Rufus gets about by running. ’ (Other days of the week)

This example is a bit weird, but it helps demonstrate the point so stick with me. When working with objects, it is important to distinguish properties and methods of an object ( e.g. an animals name and how it moves around) from variables used in the construction of those properties and methods (such as the $dayofweek and $today variables defined above). As you can see in the above example, PHP offers $this as a non-contextual reference to the current object.

The above example is fairly abstract but its helpful in getting a grasp on these concepts before moving on. Coming soon: Extending a Class, Access Modifiers.


...
Ian

share:

Ready to start your next project?

See how we can help you accelerate your business