Home arrow PHP arrow Page 4 - Back To Class

New Cars For Old - 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
Now that you've got the concepts straight, let's take a look at the nitty-gritty of a class definition.

<? class Automobile { // this is where the properties are defined var $colour; var $shape; ... // this is where the functions are defined function start() ( // code goes here ) ... } ?>
Every class definition begins with the keyword "class", followed by a class name. You can give your class any name that strikes your fancy, so long as it doesn't collide with a reserved PHP word. A pair of curly braces encloses all class variables and functions, which are written as you would normally code them.

In order to create a new instance of a class, you need to use the "new" keyword, and assign the newly-created object to a PHP variable.

<? $his = new Automobile; ?>
In English, the above would mean "create a new object of class Automobile and assign it to the variable $his".

You can now access all the methods and properties of the class via this variable.

<? // accessing properties $his->make = "Ferrari"; $his->colour = "Black"; // accessing methods $his->start(); $his->accelerate(180); ?>
Again, in English,

$his->make = "Ferrari";
would mean

"assign the value Ferrari to the variable $make of this specific instance of the class Automobile", while

$his->accelerate(180);
would mean

"execute the function accelerate() with parameter 180 of this specific instance of the class Automobile".

Note the -> symbol, and the fact that the $ symbol is omitted when accessing variables of a specific class instance.

 
 
>>> 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: