Thursday, March 20, 2008

OOP in PHP

Object Oriented Programming in PHP

This article introduces Object Oriented Programming (OOP) in PHP. Luis shows you how to code less and better by using some OOP concepts and PHP tricks.Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For example you may have a Person object to hold the data related to a person and even provide some functionality that this person may be capable of.

Object Oriented Programming has long been used in games to represent the objects such as a User or an Enemy, or even a Weapon. This amazing way of programming has proven just as useful in software and web development.

In my opinion any OOP language should have:

* Abstract data types and information hiding
* Inheritance
* Polymorphism

This can all be done using PHP classes:

x=$v;
}

function getX() {
return $this->x;
}
}

?>

Of course you can use your own nomenclature but having a standardized one is useful.

Data Members and Functions

Object Oriented Programming in PHP - Data Members and Functions
(Page 2 of 7 )

Data members are defined in PHP using a "var" declaration inside the class and they have no type until they are assigned a value. A data member might be an integer, an array, an associative array or even an object.

Methods are defined as functions inside the class, to access data members inside the methods you have to use $this->name, otherwise the variable is local to the method.

You create an object using the new operator:

$obj=new Something;
Then you can use member functions like:
$obj->setX(5);
$see=$obj->getX();


The setX member function assigns 5 to the x data member in the object obj (not in the class), then getX returns its value; 5 in this case.

You can access the data members from the object reference using for example: $obj->x=6. However, this is not a very good OOP practice.

I encourage you to set data members by defining methods to set them and access the data members by using retrieving methods.

You'll be a good OOP programmer if you consider data members inaccessible and only use methods from the object handler. Unfortunately PHP doesn't have a way to declare a data member private so bad code is allowed.

Inheritance

Inheritance is easy in PHP using the extends keyword:


class Another extends Something {
var $y;
function setY($v) {
// Methods start in lowercase then use uppercase initials to
// separate words in the method name example getValueOfArea()
this->y=$v;
}

function getY() {
return $this->y;
}
}

?>


Objects of the class "Another" now have 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 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.

No comments: