Learn how to use Object Oriented Programming in PHP

Sunday, July 3rd, 2011|Beginner tips, Misc|by Romans

Whenever I come over object-oriented tutorial or a book, it always uses either Java or C++. I have never found this a big problem, but there are many people out there, who are NOT familiar with those languages and can never find a good tutorial to Object Oriented Programming.

Another problem is that PHP has no built-in interface with the user. You need to generate HTML and it’s not easy to do so, that’s why it makes any object-oriented examples useless or silly.

I want to give you the PROPER example of a good object-oriented paradigms in PHP language. At the end I will explain how this Geometrical universe can be used for Web Software programming:

The Application – Geometry

If you look at history of languages, some languages emerged from procedural into object-oriented, such as C++, Perl and PHP. Other languages were Object-oriented initially such as Smalltalk, Ruby and Java. The most important rule when you use mixed language such as PHP for object-oriented programming is – avoid at all costs any procedural code. Therefore the only procedural code we are going to have will be:

[php]
$app = new Application();
$app -> run();
[/php]

From here on, we would need to have an “Application” class. Objects represent a real-life objects and objects of a class “Application” will represent our own application. Neat.

[php]
class Application {
public $universe=array();
function __construct(){
}
function run(){
}
}
[/php]

This is a good start. Our application would deal with geometrical objects, however due to very limited interaction will only calculate the total area of all of our geometrical objects. Let’s first define a generic object. It’s important that all the objects have a generic abstract object as it’s parent.

[php]
abstract class Shape {
public $x;
public $y;
function __construct($x, $y){
$this->moveTo($x, $y);
}
function moveTo($x, $y){
$this->x=$x;
$this->y=$y;
}
abstract function getArea();
}
[/php]

Let’s continue to define our first real usable object — a dot.

[php]
class Dot extends Shape {
function getArea(){
return 0;
}
}
[/php]

When we were creating a dot, we used the first and most important ability of Object Oriented Programming called “Inheritance”. In PHP it’s happening when you “extend” class from another. When doing so, the new class will have all the methods of the previous class. However you can add new methods or re-define existing in your new class.

In our example with Dot we have haven’t re-defined constructor or moveTo function, because they function exactly like in Shape class. getArea however needs to be re-defined. The area of a dot is always zero.

Next, let’s define a Circle class. We will also inherit it from the generic Shape class.

[php]
class Circle extends Shape {
public $radius;
function __construct($x, $y, $radius){
$this->moveTo($x,$y);
$this->setRadius($radius);
}
function setRadius($radius){
$this->radius=$radius;
}
function getArea(){
return pi() * $this->radius * $this->radius;
}
}
[/php]

For the Circle class we needed to introduce new property – radius. It comes as addition to already existing x and y. Constructor is redefined and includes the code moving arguments into the properties.

Let’s continue with more shapes.

Vector object has a coordinates for it’s start, then delta-x and delta-y to define it’s direction.

[php]
class Vector extends Shape {
public $a, $d_x, $d_y;
function __construct($x, $y, $d_x, $d_y){
$this -> moveTo($x, $y);
$this -> d_x = $d_x;
$this -> d_y = $d_y;
}
function getArea(){
return 0;
}
function getLength(){
return sqrt($this->d_x * $this->d_x +
$this->d_y * $this->d_y);
}
function getCrossProduct($v2){
return $this->d_x * $v2->d_y -
$this->d_y * $v2->d_x;
}
}
[/php]

For our vector we are adding a function getLength with returns magnitude or, in general terms, a length of a vector. The are of a vector is still zero. Let’s add few more shapes.

[php]
class Square extends Vector {
function getLength(){
return $this->d_x * $this->d_y;
}
}
[/php]

This defines Square and the next one defines triangle.

[php]
class Parallelogram extends Vector {
public $a, $b;
function __construct($x, $y, $x2, $y2, $x3, $y3){
$this->a = new Vector($x, $y, $x2 – $x, $y2 – $y);
$this->b = new Vector($x, $y, $x3 – $x, $y3 – $y);
}
function moveTo($x, $y){
$this->a->moveTo($x, $y);
$this->b->moveTo($x, $y);
}
function getArea(){
return $this->a->getCrossProduct($this->b);
}
}
class Triangle extends Parallelogram {
function getArea(){
return parent::getArea() / 2;
}
}
[/php]

Triangle ABC in our universe is defined through 2 vectors AB and AC. Also Triangle’s implementation is based on implementation of Parallelogram, since they share a lot of things in common. Now that we have plenty of objects, lets get back to our application and see how generalization works.

[php]
class Application {
public $universe=array();
function __construct(){
$this->universe[] = new Dot(3,4);
$this->universe[] = new Triangle(5,7,8,7,7,9);
$this->universe[] = new Circle(2,4,2);
}
function run(){
$area=0;
foreach($this->universe as $obj){
$area+=$obj->getArea();
}
echo "Total area: ".$area;
}
}
[/php]

Click here to download resulting file.

If everything is OK, you should be able to get value of total area of all your objects. Generalization means that inside run() we treat all our object in a generic way. We do not distinguish between different types and objects themselves make sure they properly implement the getArea() method.

Applying same for Web Development

Although this was a spectacular rehearsal in Geometry, this might not have been much of a use for Web Software development. Actually – the object-oriented model can perfectly illustrate the pattern for web development too.

What if your application consisted of generic objects – pages. Each page would worry about it’s own initialization and rendering but Application would only determine which page to display. Secondly what about elements on your pages would behave similarly to geometrical objects – inherit themselves, redefine methods and use sub-objects.

Ultimately goal of your web objects is to produce HTML and it would rely on objects contained within a certain page to assemble all the required HTML output.

The exactly this principle was a foundation for the core structure of Agile Toolkit, although there are other web framework which rely heavily on generalization of objects with aim to create HTML output of an application.