Getting Started with Objects with PHP V5
By Matt Zandstra2005-07-01
A first class
You can create a class with the
class keyword. At its simplest, a class consists of the keyword class, a name, and a code block: |
The class name can contain any combination of letters and numbers, as well as the underscore character, but cannot begin with a number.
The Dictionary class in the previous example is perfectly legal, even if it is of limited use. So how do you use this class to create some objects?
|
In form at least, instantiating an object is similar to calling a function. As with a function call, you must supply parentheses. Like functions, some classes require that you pass them arguments. You must also use the new keyword. This tells the PHP engine that you wish to instantiate a new object. The returned object can then be stored in a variable for later use.
Tutorial Pages:
» Why you need to know objects and classes, and how to use them
» What are classes and objects?
» A first class
» Properties
» Methods
» The constructor
» Keywords: Can we have a little privacy in here?
» Working in class context
» Inheritance
» Summary
» Resources
First published by IBM DeveloperWorks
