Tutorial details
Name : Classes
Author : Kenneth Clark
Language : PHP 4
Platform : Platform Independant
Click here if you require development or hosting solutions : [ SkyeTech Solutions ]

In the beginning there where functions, then there where class and I have no idea how classes became libraries for functions.

Don't get me wrong, you can use classes for anything but it is best to understand what they where designed for. The class is directly linked to Object Orientated Programming.

Now the debate rages on about the performance differences of object orientated code and procedural code. Granted procedural will run quicker BUT I don't want to be the one maintaining procedural code.

Now the object orientated ideal is to create all the components of your system in objects. This allows for operations pertinant to the object to be encapsulated inside the object. As an example. Say you have a bicycle. It can be broken into parts. You would have a wheel, a frame, brakes, handle bars, cogs, chain and peddles. Now a representation of that might be:

class Wheel{}
class Brakes{}
class HandleBars{}
class Cog{}
class Frame{}

class Bicycle{
  Wheel;
  Brakes;
  HandleBars;
  Cog;
  Frame;
}
Now the Wheel class could be broken down into tyre, spoke and rim but we will live with it the way it is. Now lets look into a real programming example. We will work with a user that belongs to a group. Very simple.
class Group{
  var $id;
  var $name;

  function Group(){} // constructor
  function Add(){ 
    //code to add group here
  }
  function Delete(){ 
    //code to delete group here
  }
}

class User{
  var userGroup;  //the object Group will be stored here
  var email;
  var password;

  function User(){
    $this->userGroup = new Group();
  } // constructor
  function Add(){ 
    //code to add user here
  }
  function Delete(){ 
    //code to delete user here
  }

}
So now when you initialize the user class it will create an instance of the group class inside it so to access the group you cna simply go
$oUser = new User();
$groupName = $oUser->userGroup->name;
There you have it. Got a question swing it our way.


SkyeTX Technologies Business Software Solutions
SkyeTX Technologies Business Software Solutions