RE: [PHP] object vs functions

2002-12-28 Thread Paul Reed
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=1;// 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




Re: [PHP] object vs functions

2002-12-28 Thread Mat Harris
thanks it does help a lot, and I wrote mine at 4:30ish too :).

does anyone know of any tutorials or example projects I can look at to find
out about classes?

thanks for the help

On Sat, Dec 28, 2002 at 04:17:02 -0500, Paul Reed wrote:
 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=1;// 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 HarrisOpenGPG Public Key ID: C37D57D9
 [EMAIL PROTECTED]  www.genestate.com   
 

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



msg90840/pgp0.pgp
Description: PGP signature