PHP5 Object Oreinted Programing or POOP

Posted by: §intå×

PHP5 Object Oreinted Programing or POOP - 01/16/07 06:57 AM

Whats up people. I have recently started getting into PHP5's OOP stuff and thought I would drop a tut. Now I will cover what I have learned and in no way am I claiming to know all. I put off learning OOP for years. I thought it is just another way to do what I am doing. My scripts are just as good.

I will not say OOP style is better, but I will tell you it has some serious benefits. For one you will re-invent the wheel much less than you do now. You probably do not even know how much redundant code you have yet.

I will cover only PHP5 as there are differences in PHP4 and PHP5.

First lets start with variables. In PHP5 OOP you can set the access levels for vars with 3 different settings. Public, Private and Protected.

Public - code outside your class can change the value of the variable. You can not count on keeping it a certain value. This is useful if you want to pass some values into the class.

Private - The only way the variable will change is inside the class. Methods inside the class can change the variable, but child classes and outside code can not.

Protected - Can be changed by the methods and objects in the class and by the classes children. **Note a parent should never know too much about its children.**

In addition to variables having these access rights, our methods can be assigned the same rights. It should be noted if you do not assign rights the default is Public. As a rule you will want to lock vars and methods down that do not need to be publicly accessed. Okay if you have never coded in an OOP style you are probably saying, WTF is this guy talking about. Lets look at some code.

PHP:

<?php
/*
    +-------------------
    |    Fish Class
    |
    |    This class will
    |    spawn a fish and
    |    show us a life cycle
    |    of the fish
    +-------------------

*/
class fish{
    Public $fishName;
    Public $fishColor;

    Public function getBirth($fishName,$fishColor){
        $birth = "A fish is born.<br/>  Its name is $fishName.<br/>  It is $fishColor<br/>";
        return $birth;
    }
}
?>

<?php
/*
    Above you have a very simple class.  
    Below is an example on how to call and use this class.
*/
$nemo = new fish;
print $nemo->getBirth('Nemo','Orange');
?>



The above code would of course print out

Code:
A fish is born.
It's name is Nemo.
It is Orange.


As I said this was a simple class. Did you notice the function in this class? That is what we will from now on refer to as a method. Methods are where actions happen. Data is processed and we get feed back.

Now you could very easily keep creating fish with the line...
PHP:
$nemo = new fish; //or $dory = new fish; //or $flounder = new fish;


Each of the above would be an instance of the class. Each independently having access to all the variables and methods in the class. Stating to see the power yet? Now our fish is born. But maybe there are some things we could automate here. What do we know about life? Well everything must die right? How about other things? Everything has a life span, all fish have fins. Lets expand our class and automate some things with special methods.

Code:
special methods

__construct
__destruct


Lets see how we can use "_ _construct" and "_ _destruct" to make our life easier.

PHP:

<?php
/*
    +-------------------
    |    Fish Class
    |
    |    This class will
    |    spawn a fish and
    |    show us a life cycle
    |    of the fish
    +-------------------

*/
class fish{
    Public $fishName;
    Public $fishColor;
    Public $fishFins;

    public function __construct(
       $fishName,
       $fishColor,
       $fishFins){
           $this->fishName  = $fishName;
           $this->fishColor = $fishColor;
           $this->fishFins  = $fishFins;
    }

    public function __destruct(){
        print "<br/><br/>This fish $this->fishName has died.<br />";  
        print "May all $this->fishFins of his fins rest in peace";
    }

    Public function getBirth($this->fishName,$this->fishColor){
            $birth = "A fish is born.<br/>";  
            $birth .= "Its name is $this->fishName.<br/>";  
            $birth .= "It is $this->fishColor.<br/>";  
            $birth .= "It has $this->fishFins fins.<br />";
        return $birth;
    }
}
?>

<?php
/*
    Above you have a very simple class.  
    Below is an example on how to call and use this class.
    Please note now we define the needed values in the object
    definition.  No longer in the print statement.
*/

$nemo = new fish(
                  'Nemo',    // Fish name
                  'Orange',  // Fish color
                  5          // Number of fins on fish
                 );
print $nemo->getBirth();
?>



The above should out put
Code:
A fish is born.  
Its name is Nemo.
It is Orange";  
It has 5 fins.

This fish Nemo has died.
May all 5 of his fins rest in peace


But wait I didn't call the __destructor method. Nope it is automatically called when the class is finished. Think about closing MySQL connections. Think about unsetting vars, arrays or unlinking temp files. This is very handy indeed. You will also notice by putting the vars in the __constructor I force the user to define these vars each time they create an object or instance of fish. This can be very handy also.

Of course you can put more methods in the class as well. Play with that I will add more later.