Helping ordinary people create extraordinary websites!

Getting Started with Objects with PHP V5

By Matt Zandstra
2005-07-01

What are classes and objects?
Put simply, a class is a discrete block or bundle of variables and methods. These components usually coalesce around a single responsibility or set of responsibilities. In this article, you will create a class that collects methods for querying and populating a dictionary of terms and values.

A class can be used directly as a neat way of organizing data and functionality, very much like a bunch of functions and variables. This is to ignore a powerful dimension, however. Classes can be used to generate multiple instances in memory. Such instances are called objects. Each object has access to the same set of functions (called methods in an object-oriented context), and variables (called properties or instance variables), but the actual value of each variable may differ from object to object.

Think about a unit in a role-playing game -- a tank, perhaps. A class may lay down a set of variables for tanks: defensive and offensive capability, range, health, and so on. The class may also define a set of functions, including move() and attack(). While the system contains one tank class, this class may be used to generate tens or hundreds of tank objects, each potentially with its own characteristics of health or range. A class, then, is a blueprint or template for use in generating objects.

Perhaps the easiest way to understand classes and objects is to create some.





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