+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: PHP 5 and OOP

  1. #1
    Jordan Guest

    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.

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Jul 2008
    Posts
    22
    Rep Power
    0

    Talking Re: PHP 5 and OOP

    great share man nice info...thanks

  4. #3
    Jordan Guest

    Re: PHP 5 and OOP

    No problem!

  5. #4
    CodeJunkie is offline Newbie
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Re: PHP 5 and OOP

    Thanks for a great guide!

  6. #5
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Re: PHP 5 and OOP

    Do you believe that we should avoid sql use in an oop application, even for complex reports, or we should use SQL when needed?

  7. #6
    Jordan Guest

    Re: PHP 5 and OOP

    Why would you want to avoid SQL use and how can you when you are using a database? Two OOP Design Patterns come to mind when databases are mentioned - Singleton and Factory design patterns. These OOP design patterns can be used for database connections and executing SQL queries. The fact that these patterns are commonly used in PHP for SQL execution show that there is no reason to mix OOP and SQL.

  8. #7
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Re: PHP 5 and OOP

    For example, I want to make a complex report that uses data from many tables with millions of records. If I want to use my classes I will need many lines of code and the performance will be not reasonable. If I use a query it will be a wrong usage of OOP?

  9. #8
    Jordan Guest

    Re: PHP 5 and OOP

    Using OOP will be cleaner and faster than using procedural method. Maybe I still don't understand what you are asking.

  10. #9
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: PHP 5 and OOP

    Quote Originally Posted by Jordan View Post
    Using OOP will be cleaner and faster than using procedural method. Maybe I still don't understand what you are asking.
    As far as I knew, OOP used more resources. Nonetheless, I do not see any reason not to use it.

  11. #10
    Jordan Guest

    Re: PHP 5 and OOP

    The reason I say faster is because he is less likely to create multiple instances of the DB connection object using procedural.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts