Home arrow PHP arrow Page 2 - Back To Class

Who Needs Class, Anyway? - PHP

Classes and objects are powerful OOP concepts - and PHP4 supportsthem too. This article explains some basic OO entities (including classes,constructors and extensibility) with examples of a table builder and aguestbook.

TABLE OF CONTENTS:
  1. Back To Class
  2. Who Needs Class, Anyway?
  3. If Wishes Were Prancing Horses...
  4. New Cars For Old
  5. Ford's Law
  6. Turning The Tables
  7. Under Construction
  8. Extending Yourself
  9. Be My Guest
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 9
October 05, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Before beginning, though, let's make sure that you have a clear idea of the concepts involved here.

In PHP, a "class" is simply a set of program statements which perform a specific task. A typical class definition contains both variables and functions, and serves as the template from which to spawn specific instances of that class.

Specific instances of a class are referred to as "objects". Every object has certain characteristics, or "properties", and certain pre-defined functions, or "methods". These properties and methods of the object correspond directly with the variables and functions within the class definition.

Once a class has been defined, PHP allows you to spawn as many instances of the class as you like. Each of these instances is a completely independent object, with its own properties and methods, and can thus be manipulated independently of other objects.

Now, you're probably wondering whether this is a little redundant, since PHP also allows you to create your own functions and use them wherever required in your code. And you're correct, to some extent - if you're only planning to spawn a single object, a class is redundant and a function will work just as well.

But there are situations where you need to spawn more than one instance of an object - for example, two simultaneous database links for two simultaneous queries, or two shopping carts. In such a situation, classes are preferred, since each instance of the class comes with its own variables and functions, and thus can be manipulated with affecting other variables within the program.

Classes also help you keep your code modular - you can define a class in a separate file, and include that file only in the pages where you plan to use the class - and simplify code changes, since you only need to edit a single file to add new functionality to all your spawned objects.{mospagebreak title=Driving Around The Bend} Let's consider a simple example, which will illustrate this better.

Take your average everyday car, that four-wheeled gasoline-chugging monster that you drive to work every day. Can you consider it, within the framework of OOP, as an "object"?

Why not? After all, every car has certain characteristics - the colour, the shape, the number of doors, the hood ornament - which are equivalent to object properties. And every car comes with certain obvious functions as well - start, stop, turn, accelerate, decelerate - all of which are equivalent to object methods.

Let's take it a little further. Every car can, in fact, be considered a subset of the class Automobile, which defines the basic characteristics and functions of every automobile on the planet. Once the class Automobile spawns a new car, or "object", its individual characteristics (color, shape) can be adjusted independent of other objects.

Now, if you sat down to code this class, it would probably look something like this:

<? class Automobile { // this is where the properties are defined var $colour; var $shape; var $size; var $number_of_doors; var $number_of_seats; // this is where the functions are defined function start() ( // code goes here ) function stop() ( // code goes here ) function accelerate($speed) ( // code goes here ) function decelerate($speed) ( // code goes here ) } ?>


 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: