#native_company# #native_desc#
#native_cta#

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

By Luis Argerich
on 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;
$obj2->setX(6);
$obj2->setY(7);
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:

<?php

class Something {         

    var 
$x;

    function Something($y) {

        
$this->x=$y;

    }

    function setX($v) {     

        
$this->x=$v;          

    }

    function getX() {

        return 
$this->x;

    }

}

?>



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″)
Then:
$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.

1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9