Advertisment

Object-oriented PHP

author-image
PCQ Bureau
New Update

PHP, a popular server-side scripting language (like ASP and JSP), had minimalist support for object-oriented programming. But, the upcoming PHP, version 5, is much touted for its object-oriented features. It supports data encapsulation and inheritance. That is, the language now supports keywords such as private, public, protected and extends (inheritance). It also supports interfaces, abstract classes, static variables and methods and final methods. 

Advertisment

We show you how to get object-oriented with PHP, syntactically. As of this writing, the first release candidate of PHP 5 is out. To begin with, we assume that you are familiar with object-oriented concepts, constructs and keywords. We also assume that you know

PHP. 

A simple OO app





class phpclass{



private $var=”Hello”;


public function getVar( ){


return $this->var;


}


}


$obj = new phpclass( );


echo $obj->getVar( );


?>






Snapshot
Applies to PHP developers
Usp PHP scripting in an object-oriented manner
Links www.php.net 
Advertisment

The above code declares a class called phpclass. The class has one private variable and a public function that returns the private variable. Note that the private variable is referred within the function getVar( ) not by its name, but as $this->var. While the way to instantiate an object named $obj should look familiar, the way to access the member function (getVar( ) ) is a little different. Those coming from C++ or Java background will be expecting a dot notation to access the member function or the method. In PHP the member method or variable is accessed using ->, as shown in the last line. 

To access the member variable $var directly change the private prefix to public and then you can access it as echo $obj->var;



You can also have public and private methods. The above code declares getVar( ) method as public. You may like to have private methods in case you want to perform some logic within the class. For example:






class phpclass{



private $var=”Hello”;


private function format( ){


$this->var = $this->var .” Shekhar”;


}


public function getVar( ){


$this->format();


return $this->var;


}


}


$obj = new phpclass( );


echo $obj->getVar( );


?>



















Note that how the private method is called using the -> operator. Also, a private method can be called only from within the class. An attempt to $obj->format() will be greeted by an error. 

A tint of inheritance



To see inheritance at work, change the private keyword to protected in the above code. Then add the following lines of code:







class phpsubclass extends phpclass{



public function getVar( )


{


return $this->var;


}


}


$obj1 = new phpsubclass( );


echo $obj1->getVar( );


?>















Note the use of extends keyword for inheritance. The syntax is: class extends


Note that the base class phpclass and the subclass both have a method called getVar( ). The subclass’s getVar( ) is said to override the base class’s getVar( ). If you don’t want the getVar( ) method in the base class to be overrided then use the keyword final in phpclass as:





public final function getVar( )







Unlike Java, PHP does not support final keyword for variables. In Java, final keyword is used for variables to declare them as constants. PHP uses the const keyword instead as: const PI=3.14;





Note that the variable is not prefixed by any access modifiers (public, private and protected). The constant can be referenced within the class (in which it is declared) by simply using its name as: echo PI;





No need to use this->PI. To access the constant outside the class use:::PI







Advertisment

For example, if PI is declared in our phpclass then it can be referenced as: echo

phpclass::PI;

Blueprints with Interfaces



Interfaces are a popular way to prescribe blueprints for your classes to other developers. Most object-oriented programming languages support it and so does PHP 5. The way to write interfaces with PHP 5 is as follows:







interface blueprint



{


function setVar($value);


function getVar();


}


class myclass implements blueprint{


private $var;


public function setVar($value){


$this->var=$value;


}


public function getVar(){


return $this->var;


}


}


?>





















Note the use of implements keyword to declare that myclass provides the implementation for the blueprint interface. The blueprint interface defines two methods called setVar( ) and getVar( ) whose implementation must be coded in the class that implements it. If the class does not implement one or more methods and if the method’s signatures do not match with the ones declared in the interface, then an error will show up. 

And some Abstraction 



Abstraction is similar to the concept of interfaces but with an abstract class one can implement some of the methods while leave the others to be implemented. For example:






abstract class blueprint{


protected $var;


public function setVar($value){


$this->var=$value;


}


public abstract function getVar();


}













A class which inherits the above defined abstract class named blueprint, must provide the implementation for the function getVar(). For example:





class phpclass extends blueprint{


public function getVar( ){


return $this->var;


}


}











You cannot create objects for abstract classes. That is:






$obj = new blueprint( );








will flash an error. The correct way is to create objects for the inherited or derived class like:






$obj = new phpclass( );







Bottom line, PHP 5 is all set to quench your thirst to employ object oriented methodology for coding web based applications. 



Shekhar Govindarajan



T4Enterprise

Advertisment