Helping ordinary people create extraordinary websites!

Getting Started with Objects with PHP V5

By Matt Zandstra
2005-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:


class Dictionary {

}

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?



$obj1 = new Dictionary();
$obj2 = new Dictionary();
$obj3 = new Dictionary();

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:

First published by IBM DeveloperWorks


 1 Votes

You might also want to check these out:


Leave a Comment on "Getting Started with Objects with PHP V5"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS