A detailed introduction into object oriented programming for PHP developers looking to expand their skills. OOP in PHP is the next step for any developer wanting to improve their code.
Why is learning object oriented programming so hard?
Learning object oriented programming is the next logical step for any web developer wanting to advance their career. Many website developers put off this step either because they don’t see its relevance or because they find it confusing. The main reason I noticed when I first started out was lack of use. Because there are alternative ways of writing code, individuals would rather not use OOP and use the procedural alternative, and as most of us know, the only way to advance knowledge of programming is through actually programming. It wasn’t until I started to learn C# that I actually started to apply OOP to my PHP work, mostly because in C# you have to use OOP so I picked it up very quickly.
I’m going to go over the basics of object oriented programming with PHP and hopefully clear up some of the confusion by using examples that relate directly to what you are probably doing on a daily basis as a PHP programmer. Once you start picking up these skills, make sure to USE THEM on a daily basis and everyday implement something new related to OOP or you will definitely never really learn to be effective with it. If you are reading this, I’m going to assume you probably now what OOP is and have tried to learn it before. I’m not going to go over what OOP is, just HOW to use it with real world examples in PHP.
A Useful CSS Class
This class will allow you to develop an application to create CSS dynamically depending on whatever input you give it.
class DynamicCSS
{
protected $lineOfCSS = array();
public function addCSS($property, $value)
{
$this->lineOfCSS[] = array("property" => $property, "value" => $value);
}
public function run()
{
foreach ($this->lineOfCSS as $array) {
$this->outputCSS($array['property'], $array['value']);
}
}
public function outputCSS($property, $value)
{
echo "{$property}: {$value};";
}
}
$css = new DynamicCSS();
$css->addCss("display", "none");
$css->run();
This class has an array named lineOfCSS, that will hold each line of CSS that we give it. It has the addCSS method that will handle adding the line to the array. It also has the run method, which will iterate through the array and pass the line to the outputCSS method, which will output it to the screen.
The $this keyword is used to signify the use of a variable related directly to the specific instance of an object from the class you are currently using. If you have multiple objects from the same class, they all have their own variables and do not influence one another.
Interfaces
Think of an interface as a blueprint of methods that your classes can use. It is a skeleton that you can build off of later. Think of an interface as a way to link classes that are similar. Interfaces ensure that a class always has a specific method and that you never get a method undefined error.
To use an interface, your class must implement it.
interface dbConnections {
function update();
function select();
}
class Db implements dbConnections {
public function update() {
// update code here
}
protected function select() {
// select code here
}
}
The interface makes sure that the Db class has an update a select method.
This example brings us to our final example every developer should know.
The private, protected, and public keywords.
Private Keyword
If a variable or method is labeled private, it can only be called from the class that it is declared in.
Protected Keyword
Protected variables and methods can be called from the class they were declared in and any class that is extended from this class.
Public Keyword
Variables and methods labeled public can be called from anywhere in the script.
Final Keyword
The final keyword is used on methods to ensure that is cannot be changed or overwritten. The following code will throw and error.
class Display
{
final public function image()
{
echo "<img src='img.jpg'/>";
}
}
class DisplayAgain extends Display
{
public function img()
{
echo "<img src='img2.jpg'/>";
}
}>
I hope this introduction to object oriented programming has cleared up any confusion that you may have had. I’m going to turn this into a series now that the foundation has been set that will integrate more OOP and PHP topics to improve your website development skills.


