Objects are not simply a 'grouping' of functions, it's a fundamentally
different approach to programming where variable and functions are
grouped into objects. (OOP, or Object Oriented Programming)

Objects contain all the necessary variables to hold the information on
an item, and all the functions that work on that particular item.

A class is simply a 'blue-print' for an object. (each object is made
from that blueprint.)

Say for instance you were doing a used auto site, and this site had many
cars of different makes and models, and you wanted to assign a different
markup to each car.

Class c_Car
{
  var $make;
  var $model;
  var $color;
  var $cost;
  var $markup;

  function price()
  {
    // '$this->cost' means the $cost variable in the current object.
    $price=$this->cost+($this->cost*$this->markup);
    return $price;
  }
}

// so I have 2 cars....

$car1=new c_Car; 
$car1->cost=10000;    // what the car cost you.
$car1->markup=0.05;   // 5% markup from cost for this car.

$car2=new c_Car;
$car2->cost=7500;     // what the car cost you.
$car2->markup=0.10;   // 10% markup from cost for this car.

echo $car1->price();  // output the price of car1, (10500).
echo $car2->price();  // output the price of car2, (8250).

As you can see, the function works internally to the object.

To learn more, you should really read up on OOP. Overall it's an easier
way to do most sites, but sometimes a simple collection of functions in
an include file may be more practical (usually for sites with very
little PHP).

In the last few months, I have shifted my own programming techniques to
use classes and I have found it tends to work better for almost every
situation. Personally, I wish I had found out about them sooner!

Hope this helps, (And I hope it makes sense, as I'm writing at 4:05am!)

Paul Reed.






-----Original Message-----
From: Mat Harris [mailto:[EMAIL PROTECTED]] 
Sent: Friday, December 27, 2002 21:18
To: [EMAIL PROTECTED]
Subject: [PHP] object vs functions

if i have a php script containing some functions, then all i have to do
is
include() the file and then call the functions.

if i had an object (taking the example from php.net):

<?php
class foo
{
        function do_foo()
        {
                echo "Doing foo."; 
        }
}

$bar = new foo;
$bar->do_foo();
?>
                     
what is the point of having the object, when i could just call the
functions?

what are the uses of objects over functions/groups of functions?

sorry if this is an innane or frequently asked question.

-- 
Mat Harris                      OpenGPG Public Key ID: C37D57D9
[EMAIL PROTECTED]        www.genestate.com       



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to