|
|||||
|
PHP and object oriented programming:
1.0 Introduction: Object oriented programming was introduced for the first time in PHP 3. But the subjects is extremely actual again in PHP 5, because the entire concept of the OOP(Object Oriented Programming) was in fact redesigned from scratch for the new version of the language (PHP 5). Hence, this tutorial would be interesting for the advanced user familiar with PHP 4 and as well for the beginners, since we will try to use more "understandable" words and examples comprehensive enough to express the real power of the object oriented programming. 1.1 What is the Object Oriented programming? The main difference between the functional programming and the object oriented programming is that in the OOP the data is all bundled into one piece, called "object". The OOP brings clarity to the software arguments. It is however, difficult to consider the OOP more powerful than the functional programming. On the other hand, the OOP and its main entity the "class" relate to much harder programming paradigms and solving harder programming dilemmas. Perhaps, the power behind the OPP is its capability to be simply involved in any place of the program as a separate entity. For example, every class is a standalone argument, which at the same time relates strictly to a set of other classes. Below we will try to explain in simple words the OOP characteristics. ![]() The following is a simple but powerful example of class. It defines the code, creates instances of it(2 in the case), sets the name of each instance and then prints the names: Code:
class Person {
private $name;
function setName($name)
{
$this->name = $name;
}
function getName()
{
return $this->name;
}
};
$chris = new Person();
$chris->setName("Chris");
$johny = new Person();
$johny->setName("Johny");
print $johny->getName() . "\n";
print $johny->getName(). "\n";
To declare a class you use the "class" keyword, in this way: Code:
class Person
{
class YetAnotherClass
{...}
}
1.3 Polymorphism: In brief "polymorphism" is a way to inherit a child class from the parent class without the need to rewrite the code from scratch. Here is a good example: Code:
class Cat
{
function miau()
{
print "miau";
}
}
class Dog
{
function wuff()
{
print "wuff";
}
}
function printTheRightSound($obj)
{
if ($obj instanceof Cat)
{
$obj->miau();
}
else if ($obj instanceof Dog)
{
$obj->wuff();
} else
{
print "Error: Passed wrong kind of object";
}
print "\n";
}
printTheRightSound(new Cat());
printTheRightSound(new Dog());
Code:
class Animal {
function makeSound()
{
print "Error: This method should be re-implemented in the children";
}
}
class Cat extends Animal {
function makeSound()
{
print "miau";
}
}
class Dog extends Animal {
function makeSound()
{
print "wuff";
}
}
function printTheRightSound($obj)
{
if ($obj instanceof Animal) {
$obj->makeSound();
} else {
print "Error: Passed wrong kind of object";
}
print "\n";
}
printTheRightSound(new Cat());
printTheRightSound(new Dog());
![]() In the diagram above would be logical to use setCenter in class "Form" and leave the rest method called "draw" to the derived classes "square" and "circle". However, in order to do so you need to specify the "draw" method as an abstract method so that PHP will know that it doesn't belong exclusively to the very parent class. Then automatically the parent class "form" becomes an "abstract" class, meaning that simply it isn't a "normal" class, but rather it is used for inheritance only. Though this is a powerful feature, sadly you cannot do much more with the parent class, such as to instantiate it for example. 1.5 Cloning objects: A major difference between the OOP in PHP 4 and PHP is how the objects are threatened as their values. Pretty much like other languages like for example C# where there is a difference between a value type and reference type. Here the difference is stated in the semantics that in PHP 4 when you declare a new object, then it automatically achieves a value and it(the object) is the value itself. Now in PHP 5 the object is even more similar to an SQL database itself in terms that when you declare an object using the "new" keyword a special ID is assigned to the object, making the object similar to the so called "reference" type in other languages. 1.6 Constructors: The constructors are methods which give you the possibility to assign a value to a property: Code:
class person {
var $name;
function __construct($persons_name) {
$this->name = $persons_name;
}
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
1.7 Destructors: The destructors are the opposite of constructors, they destroy the object when you decide that it(the object) is not necessary anymore:: Code:
class MyClass
{
function __destruct()
{
print "An object of type MyClass is being destroyed\n";
}
}
$obj = new MyClass();
$obj = NULL;
2.0 Final difference between the traditional PHP functional programming and the OOP: Although the OOP lesson could continue forever, we better leave it with the mentioned in the above lines, since most of the other tasks in OOP for PHP are not actually that practical. For example in PHP you will hardly have the need to override methods, unlike C++ for example where this is 99% obligatory most of the time. Not to mention that OOP style of programming is perhaps used in less than a 1% of all opensource PHP applications on the web. The power behind OOP was designed and it is yet the very best choice for the desktop programming. But...you can experiment of course and even write OOP applications as powerful the traditional PHP functional programming software.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog Don't hesitate to ask any questions that you have! Check out our ASCII Calculator! |
| Sponsored Links |
|
|
![]() |
| Tags |
| php oop |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|