PHP: Object Oriented Programming

Object orientation is probably the area that has been subject to the most significant and far-reaching changes with the advent of PHP 5. Rather than making things incompatible with previous versions of the language, however, they enhance PHP 4’s meagre OOP offerings and make PHP 5 a fully functional object-oriented language—plus, of course, they make your life easier.

OOP Fundamentals
While the goal of this blog is not to provide a guide to the concepts of object-oriented programming, it’s a good idea to take a quick look at some of the fundamentals. OOP revolves around the concept of grouping code and data together in logical units called classes. This process is usually referred to as encapsulation, or information hiding, since its goal is that of dividing an application into separate entities whose internal components can change without altering their external interfaces.

Thus, classes are essentially a representation of a set of functions (also called methods) and variables (called properties) designed to work together and to provide a specific interface to the outside world. It is important to understand that classes are just blueprints that cannot be used directly—they must be instantiated into objects, which can then interact with the rest of the application. You can think of classes as the blueprints for building a car, while objects are, in fact, the cars themselves as they come out of the production line. Just like a single set of blueprints can be used to produce an arbitrary number of cars, an individual class can normally be instantiated into an arbitrary number of objects.

Declaring a Class

The basic declaration of a class is very simple:

class myClass {
// Class contents go here
}

As you have probably guessed, this advises the PHP interpreter that you are declaring a class called myClass whose contents will normally be a combination of constants, variables and functions (called methods).

Instantiating an Object
Once you have declared a class, you need to instantiate it in order to take advantage of the functionality it offers. This is done by using the new construct:

$myClassInstance = new myClass();

In PHP 5, objects are treated differently from other types of variables. An object is always passed by reference (in reality, it is passed by handle, but for all practical purposes there is no difference), rather than by value. For example:

$myClassInstance = new myClass();
$copyInstance = $myClassInstance();

In this case, both $myInstance and $copyInstance will point to the same object, even though we didn’t specify that we wanted this to happen by means of any special syntax. This is the standard behaviour of objects in most languages, but wasn’t the case in PHP 4, where objects were handled like any other normal variables and were, therefore, passed by value.

Class Inheritance
One of the key fundamental concepts of OOP is inheritance. This allows a class to extend another class, essentially adding new methods and properties, as well as overriding existing ones as needed. For example:

class a {
function test()
{
echo "a::test called";
}
function func()
{
echo "a::func called";
}
}

class b extends a {
function test()
{
echo "b::test called";
}
}

class c extends b {
function test()
{
parent::test();
}
}

class d extends c {
function test()
{
b::test();
}
}

$a = new a();
$b = new b();
$c = new c();
$d = new d();
$a->test(); // Outputs "a::test called"
$b->test(); // Outputs "b::test called"
$b->func(); // Outputs "a::func called"
$c->test(); // Outputs "b::test called"
$d->test(); // Outputs "b::test called"

Class Methods and Properties
As we mentioned earlier, classes can contain both methods and variable s (properties). Methods are declared just like traditional functions:

class myClass {
function myFunction() {
echo "You called myClass::myFunction";
}
}

From outside the scope of a class, its methods are called using the indirection operator ->:

$obj = new myClass();
$obj->myFunction();

Naturally, the $obj variable is only valid within the scope of our small snippet of code above—which leaves us with a dilemma: how do you reference a class’ method from within the class itself? Here’s an example:

class myClass {
function myFunction() {
echo "You called myClass::myFunction";
}
function callMyFunction() {
// ???
}
}

Clearly, callMyFunction() needs a way to call myFunction() from within the object’s scope. In order to allow for this to take place, PHP defines a special variable called $this; this variable is only defined within an object’s scope, and always points to the object itself:

class myClass {
function myFunction($data) {
echo "The value is $data";
}

function callMyFunction($data) {
// Call myFunction()
$this->myFunction($data);
}
}

$obj = new myClass();
$obj->callMyFunction(123);


This will output The value is 123.

Share this

0 Comment to " PHP: Object Oriented Programming "

Post a Comment