Lost Password?

  #1 (permalink)  
Old 04-04-2008, 05:06 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 25
Posts: 4,565
Last Blog:
PHP: list()
Rep Power: 50
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default PHP 5 and OOP

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";
1.2 Declare a class:

To declare a class you use the "class" keyword, in this way:

Code:
class Person 
      
      {
      class YetAnotherClass
      {...}   
      }
As you can see a class can contain another class, in fact there is no limitation about how many classes can consist one into another...

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());
You can now see that the example above although completely right, is not extensible. In order to add a new animal plus its property you need to write several more "else ifs" for each animal plus new function. Now below we change this code, using polymorphism. We do this, when we add a new parent class called Animal. All other classes are derived from this class:

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());
1.4 Class hierarchy.



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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply

Tags
php oop



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 01:47 AM.

Contest Stats

dargueta ........ 93.00000
John ........ 87.50000
Xav ........ 70.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads