[thelist] php design question

Andy Warwick mailing.lists at creed.co.uk
Mon Nov 18 18:47:01 CST 2002


On Monday, November 18, 2002, at 11:57 PM, Andrew Forsberg wrote:

> On Monday, November 18, 2002, at 11:27 PM, rudy wrote:
>>
>> could somebody please explain (offlist is okay, because obviously i'm
>> slow)
>>
>> how is an object different from an include?
>>
>
> ooh, ooh, this is always explained by analogy, so can i have a go?
> please?
>
> let's say we have a class called 'cocktail', and we have another class
> called 'martini'

Just seen the new James Bond movie Andrew?

Another huge advantage with classes, as a pose to includes, is to avoid
variables having the same name. This is especially common when you have
lots of programmers working on a single site, or you start to use third
party code.

Imagine you are putting together a website for a garage, and you want
to keep track of a number of different vehicles and their data.

Using includes you might call 'car.php' and 'motorbike.php' into you
logic. These were written months apart, and what you have forgotten is
that both include files have functions 'change_tyre' that act on a
variable 'wheel'. The next thing you know that nice Harley is sporting
tyres from a Nissan Micra.

By using classes you 'hide' the wheel variables and 'change_tyre'
methods, so that they are only addressed though an instance of the
class; that is a single, specific example of the generic class.

So, in PHP, you would say:

	$nissan_micra = new Car () ;

	$harley = new Motorbike () ;

	$nissan_micra -> change_wheel ( 'front-left' ) ;

	$harley -> change_wheel ( 'rear' )

The bike class could have a subtly different change_wheel method, so
that it knows it only has two wheels and would throw an error if you
tried to change a third. From the point of view of the programmer,
however, he only has to remember to call the 'same' change_wheel
function 'through' the instance of the class.

If at a later date, you modify the motorbike class so that it deals
with trikes, there is no way you can mess up any of your car logic, as
it has been kept apart.

Or maybe you want to keep track of the body color...

	$nissan_micra -> paint_body ( 'green' ) ;

	$nissan_micra_two = new Car () ;

	$nissan_micra_two - > paint_body ( 'red' ) ;

In this case, each nissan is an instance of the generic car class, and
has a discrete, independent 'body_color' variable.

And as long as you only address the body_color variable through a
method acting on that particular nissan, you don't even have to know
whether the painting is being done by hand or with a spray can.

When you are dealing with lots of 'copies' of the same things, objects
and classes will make you life a lot easier.

In the context of a web page, for instance, you normally create a page
with multiple queries to a database; if you keep the results of each
query in an object, it makes it much easier to avoid accidently
deleting a result set, or moving the pointer in a loop when you are
displaying the results.

HTH

--
Andy Warwick

Creed New Media Design, Nottingham, UK
[t] +44 (0)115 8476867
[w] <http://www.creed.co.uk/src/evolt/index.htm>




More information about the thelist mailing list