Home arrow PHP arrow Page 7 - Back To Class

Under Construction - 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
It's also possible to automatically execute a function when the class is called to create a new object. This is referred to in geek lingo as a "constructor" and, in order to use it, your class definition must contain a function with the same name as the class.

For example, if you'd like your car to start automatically when you create it, add the function Automobile() to your definition, as show below.

<? class Automobile { // constructor function Automobile { $this->start(); } function start() ( // code goes here ) } ?>
Or, in the table example you just saw, you could define a default grid and colours for your table when the object is created.

<? class Table { // constructor function Table() { $this->rows = 4; $this->columns = 5; $this->bcolor = "black"; $this->fcolor = "white"; $this->font = "Times New Roman"; } // other functions ?>
And now, when you create a new Table object like this, without defining any parameters,

<? // first table $alpha = new Table; $alpha->drawTable(); ?>
you'll get a default 4x4 black and white grid. Try it and see for yourself!

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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: