Home      Technologies      Contact Us  
Date: Wed Nov 20, 2002 7:17 am
Topic: Article 4: Intro to Object Oriented PHP - Part 1
This article will help beginner/intermiate PHP programmer help get a better grasp of 
programming in PHP using object oriented metholodigies.
This will be a 2 part article... As Ive had the 1st part written for a long time now, but 
haven't finished the 2nd part yet.. So instead of letting it sit here, I'm putting it out.
Object Oriented programming is very important as once you understand these 
concepts, you can easily apply them to other languages such as java. Myself learned 
PHP before java and find it very interesting to compare some of the similarities.
Now... Let's start into some object oriented basics.
The idea of object orientation is to reference each item/entity/thing as an object... 
Each object has certain characteristics and capabilities... In order to create objects 
in the programming language you must define classes that will eventually be 
instanticated into an object and can be manipulated.
Take a car for example..
A car has certain characteristics such as cost, make, model, year.. And has such 
capabilities such as stop, go, accelelerate, depreciate ( ;) ), etc...
So do most things in the real world... For example, let's write a sample fictional 
class for this car.. Call this upcoming code "car_class.php" and save it.
	
Code/Quote:
<?
class car {
  // These are characteristics/properties of the object, they are turned into variables
  var $cost;
  var $make;
  var $model;
  var $year;
  // A function inside a class with the same name as the class is called the 
  // constructor..  It is automatically run when the object is instantiated 
  // (loaded).. In this case the function will do nothing..  But it can be very 
  // useful to have the constructor perform operations
  function car() {
    // Nothing
  }
  // Set the cost of the car by being passed an external variable
  // ideally you'd want add checking on all function that try to set/ update
  // variables inside the class
  function setCost($extcost) {
    $this->cost = $extcost;
  }
  // A very basic function that returns the cost of the car... If it hasn't been set it will
  // in fact return nothing.. this is where added checking in the future would come in
  function getCost() {
    return $this->cost;
  }
  // I'm not going to comment anymore on the next bunch of function as they get set or
  // retrieve data from the class
  function setMake($extmake) {
    $this->make = $extmake;
  }
  function getMake() {
    return $this->make;
  }
  function setModel($extmodel) {
    $this->model = $extmodel;
  }
  function getModel() {
    return $this->model;
  }
  function setYear($extyear) {
    $this->year = $extyear;
  }
  function getYear() {
    return $this->year;
  }
  // Now onto something more intresting.. a function that actually performs some kind 
  // of action or transformation on the class/object data
  function calcCarValueInYears($futureyears) {
    return $this->cost - ($this->cost * (0.15 * $futureyears));
  }
  // End of class
}
?>
	
-----
Now let's play with that a bit here's a sample test script.. save it as test.php and run it:
	
Code/Quote:
<?
include("car_class.php");
$car = new Car();
$car->setCost(30000);
$car->setMake("Ford");
$car->setModel("Mustang");
$car->setYear(2002);
echo "<br><br>";
echo "I just bought a nice " . $car->getMake() . " " . $car->getModel() . "<br>";
echo "Got a great deal, only $" . $car->getCost() . "<br>";
echo "Unfortunately due to depreciation it'll only be worth:<br>";
for ($X = 1 ; $X <= 3 ; $X++) {
  echo "$" . $car->calcCarValueInYears($X) . " in " . $X . " years.<br>";
}
echo "<br>Thats life!<br>";
?>
	
In most applications you can pretty much assume there will be an object/class 
for almost each table in the database.. such as in the case of an ecommerce 
store that sells products, you might have such classes as: item and category.
More to come...
	
 
Managed With Tymbrel