PHPBuilder - Object Oriented Programming in PHP: The Way to Large PHP Projects Page 3

RSS Twitter

Object Oriented Programming in PHP: The Way to Large PHP Projects - Page 3

by: Luis Argerich
January 25, 2008

Objects of the class "Another" now has all the data members and methods of the parent class (Something) plus its own data members and methods. You can use:
$obj2=new Something;
Multiple inheritance is not supported so you can't make a class extend two or more different classes.
You can override a method in the derived class by redefining it, if we redefine getX in "Another" we can't no longer access method getX in "Something". If you declare a data member in a derived class with the same name as a data member in a Base class the derived data member "hides" the base class data member when you access it.
You might define constructors in your classes, constructors are methods with the same name as the class and are called when you create an object of the class for example:


class Something {         

Something($y) {

setX($v) {     

getX() {

So you can create an object by:
$obj=new Something(6);
And the constructor automatically asigns 6 to the data member x. Constructors and methods are normal php functions so you can use default arguments.
function Something($x="3",$y="5")
$obj=new Something();    // x=3 and y=5
$obj=new Something(8);   // x=8 and y=5
$obj=new Something(8,9);   // x=8 and y=9
Default arguments are used in the C++ way so you can't pass a value to Y and let X take the default value, arguments are asigned form left to right and when no more arguments are found if the function expected more they take the default values.

« Previous Page
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Luis Argerich



(Maximum characters: 1200). You have characters left.