Go Back   CodeCall Programming Forum > Software Development > Tutorials > PHP Tutorials
Register Blogs Search Today's Posts Mark Forums Read

PHP Tutorials PHP Tutorials

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-04-2008, 06:06 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-17-2008, 10:40 AM
lowrider.v.7.0's Avatar
Newbie
 
Join Date: Jul 2008
Posts: 22
lowrider.v.7.0 is an unknown quantity at this point
Talking Re: PHP 5 and OOP

great share man nice info...thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-17-2008, 10:43 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: PHP 5 and OOP

No problem!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-28-2008, 04:43 PM
Newbie
 
Join Date: Jul 2008
Posts: 4
CodeJunkie is an unknown quantity at this point
Re: PHP 5 and OOP

Thanks for a great guide!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-15-2008, 08:05 AM
Newbie
 
Join Date: Aug 2008
Posts: 2
Rodrigo Robles is an unknown quantity at this point
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-15-2008, 08:39 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-15-2008, 10:25 AM
Newbie
 
Join Date: Aug 2008
Posts: 2
Rodrigo Robles is an unknown quantity at this point
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-15-2008, 10:51 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-15-2008, 01:00 PM
John's Avatar
Co-Administrator
 
Join Date: Jul 2006
Age: 21
Posts: 5,835
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
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.
__________________

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-15-2008, 01:06 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
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



All times are GMT -5. The time now is 09:28 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0