Object Oriented Programming In PHP

If you’re familiar with Java, you probably understand the key concepts behind object-oriented programming. If you don’t however, it can be looked at as modeling data differently.

A lot of languages are natively functional. That means that multiple languages lie within functions, and those functions are executed (once or multiple times) in a particular order. Object-Oriented programming, however, approaches things differently. Instead of functions, you have methods, and methods are grouped together in what’s called a class.

Object oriented programming in php is pretty simple. Methods are basically the same as functions, but in object-oriented programming, you have the ability to easily share data between functions within the same group, or class.

You can think of it like cars. If you want a car, and you say “give me a car”, you might not know what to expect. It could be an SUV, sedan, convertible, etc. So to produce reliable results, you would make a class called “Car”, and a method within that class for each of the different types.

That gives you the ability to write something like this:

$car = new Car();

$car->getSUV();

That first line creates a new object called Car, and assigns it to the variable $car. The next line just calls a method, called “getSUV” for that Car object.

Now here’s what it would look like if you wanted a convertible instead:

$car = new Car();

$car->getConvertible();

It’s that simple. Now you just need to add the PHP that actually gives you the car. Here’s what that looks like:

class Car {

public function __constructor(){

}

 

public function getSUV(){

// some logic to get the SUV

}

 

public function getConvertible(){

// some logic to the the convertible

}

}

This may seem like a lot at first, but let’s break it down. Here we have 3 methods, which are done the same way you would normally do functions. What’s unique to methods over functions is that word “public” in front of the method declaration. This tells your code what has access to this method. For now, let’s just leave them all pubilc, so that any code can access that method.

Now we just enclose the methods in a class, and we give the class a name. But what’s that first method? This is called a constructor, and it is executed every time a Car is initialized. So when you type this:

$car = new Car();

It calls the constructor. For now the constructor doesn’t do anything, so lets add some code to made it do something.

class Car {

public function __constructor(){

$color = “red”;

}

public function getSUV(){

// some logic to get the SUV

}

public function getConvertible(){

// some logic to the the convertible

}

}

Okay, cool! Now our car is red. But wait! How do the other methods know to get a red car? That’s simple, we just assign the color variable to the class, and then we can use it anywhere in the class.

class Car {

public function __constructor(){

$this->color = “red”;

}

public function getSUV(){

$color = $this->color;

// some logic to get the SUV

}

public function getConvertible(){

$color = $this->color;

// some logic to the the convertible

}

}

Now our car is red, whether it’s an SUV or a convertible. Let’s take it a step further and allow whoever is asking for the car to choose the color for us.

class Car {

public function __constructor($color){

$this->color = $color;

}

public function getSUV(){

$color = $this->color;

// some logic to get the SUV

}

public function getConvertible(){

$color = $this->color;

// some logic to the the convertible

}

}

And to pass the color into the constructor, all we do is this:

$blueCar = new Car(“blue”);

$blueCar->getSUV();

This covers the basics of object-oriented programming, and how to implement it in PHP. It has a wide range of uses. For example, if you want to implement a reusable A/B testing framework, you would just make an ABTest class, with methods such as getOptions and recordClick.