Object Oriented PHP-GTK2 - OOP Basics Chapter 1 Object Oriented PHP-GTK2 - OOP Basics Chapter 1
it's me, bob. lol. Object Oriented PHP-GTK2 OOPS

OOP Basics Chapter 1

The basics PHP5 object orientation, essential to successful PHP-GTK

Topic: PHP5 Object Orientation (View All Tutorials)
Keywords: php oop class object property method
Updated: 280 Days Ago, 2007/11/14 20:39

In This Document
Terminology
Basic OOP Code
$this Or That
Private Data
Wrapping Up

There are two main styles of programming, procedural and object oriented. If you are reading this to learn what object orientation is then chances are you have been programming in a procedural format. I have been quoted many times for saying how much I disliked OOP programming, however this was before PHP5. The differences between PHP4 and PHP5 are outstanding, and mostly non-portable between the two versions.

Object orientation provides many benefits to helping you develop your programs as best as possible.

Easy Relations. You can think of your programming as having actual objects in it, as literal as the objects sitting on your desk right now.

Data Portability. If you develop your application right, you can get rid of all data globalization in the root scope of your program. This not only is cleaner but also could help enhance the security of your application. Also instead of passing long lists of arguments, you could simply pass an object and have all the data about it in one nice group.

Terminology

  • Object - A literal instance of data. Your mouse, keyboard, lamp... each of these are objects in the physical world, and can be objects in your programming.
  • Class - This is the definition of an object. A class states what the object has or can do. An object could have colour, or an object might have the ability to switch on or off.
  • Instance - An object is an instance of a class. Got it?
  • Method - A function which is defined in a class. It allows objects to perform actions.
  • Property - A variable storing useful information within a class. These would store information such as the colour of an object.

 

Basic OOP Code

<?php
class LavaLamp {
    public 
$colour1;
    public 
$colour2;
    public 
$colour3;
    public 
$active;
    
    public function 
toggle() {
        if(
$this->active) {
            
$this->active false;
        } else {
            
$this->active true;
        }
    }
    
    public function 
set($state) {
        
$this->active $state;
    }
}
?>

Here I have created a class which describes a Lava Lamp. The lava lamp has three different colours as you can see, and also can be turned on and off via a toggle switch, or forced on/off.

Notice how we define and use the $active property differently?

A lava lamp just sitting on the table can be seen and used by anyone in the same room, and the same is true for our eLavaLamp. I will explain.

In this class, what are the methods? What are the properties?

Properties: $colour1, $colour2, $colour3.

Methods: toggle(), set().

Notice the keywords public? This means that we are allowed to edit the properties or use the methods from outside of the class definition. If we were to have used private instead, we would not be able to read or write to the properties from outside the class. PHP also has one more keyword, protected, which will be covered in another tutorial.

So how do we go about actually using our Lava Lamp?

<?php
$lamp 
= new LavaLamp;
$lamp->colour1 'blue';
$lamp->colour2 'green';
$lamp->colour3 'silver';
$lamp->set(true);

if(
$lamp->active) {
    echo(
"the lamp is on.\n");
} else {
    echo(
"the lamp is off.\n");
}
?>

Here I created a new instance of a Lava Lamp, set the colours, and turned it on. We can then check the status of the $active property to test if the lamp was turned on or off. What if you need more than one? Then create more than one. You could even have an array of objects if you really needed.

We can use the print_r() function to examine our object.

<?php
print_r
($lamp);
?>

//. bob@elenothar [/]$ php ~/lava lamp.php
LavaLamp Object
(
    [colour1] => blue
    [colour2] => green
    [colour3] => silver
    [active] => 1
)

 

$this Or That

Referring back to the Lava Lamp class, notice that from within the methods how the property active was referenced, with the prefix $this->? Inside the class methods we can access the current object though the internal $this object. You can even access class methods this way, just as on a normal instance of the object outside the class scope.

However when outside the class, we access the properties through the actual instance of the object, in this case $lamp.

 

Private Data

First, lets redefine our Lava Lamp class.

<?php
class LavaLamp {
    public 
$colour1;
    public 
$colour2;
    public 
$colour3;
    private 
$active;
    
    public function 
toggle() {
        if(
$this->active) {
            
$this->active false;
        } else {
            
$this->active true;
        }
    }
    
    public function 
set($state) {
        
$this->active $state;
    }
    
    public function 
is_on() {
        return 
$this->active;
    }
}
?>

Notice now we have a private property $active, where before it was public. It works the same, it still stores whether the lamp is on or off but now because it is private, we cannot just test the lamp's current state by reading that property. If we did, this is what would happen.

PHP Fatal error: Cannot access private property Lava Lamp::$active

Because of this I have created the reader method called is_on() and because it is inside the class, it is allowed to read the private property. Only the set() and toggle() methods are able to edit that property, and only is_on() can tell you how it is set.

<?php
if($lamp->is_on()) {
    echo(
"the lamp is on.\n");
} else {
    echo(
"the lamp is off.\n");
}
?>

Here is how our updated LavaLamp class looks when fed to print_r().

LavaLamp Object
(
    [colour1] => blue
    [colour2] => green
    [colour3] => silver
    [active:private] => 1
)

 

Wrapping Up

That pretty much wraps up the basics of PHP Object Orientation. One last note, it is good practice that you destroy or delete an object instance when you no longer need it. You can do this by setting your instance to null, or running it through the unset() function. This allows PHP to free and reuse the system's RAM. This will become really important to remember when you start dealing with PHP-GTK.

<?php
unset($lamp);
?>

Was this document helpful? I appreciate your feedback.
What are the rules about reusing this code?


i can has web two point ooh // copyright © 2007-2008 bob majdak jr
[ xhtml css | firefox ie7 opera ]