PHP 5/MySQL Programming- P56:computer programming has often been seen as a difficult and arcane skill. Programming languages are difficult and complicated, out of the typical person’s reach. However, the advent of the World Wide Web has changed that to some extent. It’s reasonably easy to build and post a Web page for the entire world to see. The language of the Web is reasonably simple, and numerous applications are available to assist in the preparation of static pages | 253 Adding Methods to a Class To make a class really interesting it needs to have some sort of behavior as well as data. This is where methods come in. I ll improve on the simple Critter class so it has methods with which to manipulate the name property. Here s the new code found in doctype html public - W3C DTD HTML EN html head title Critter title head body Adding methods define the critter class class Critter var name Chapter 7 Writing Programs with Objects function construct handle anonymous this- name handle end constructor function setName newName this- name newName end setName function getName return this- name end getName end Critter class make an instance of the critter theCritter new Critter print original name print Initial name . theCritter- getName . br n 254 PHP 5 MySQL Programming for the Absolute Beginner print Changing name. br n theCritter- setName Melville print New name . theCritter- getName . br n body html This code produces the output indicated in Figure . figure This Critter can change his name on-the-fly. The basic technique for creating methods is to build a function within the context of a class definition. That function then becomes a method of the class. Building a Constructor The first function defined in most classes is called the constructor. Constructors are special methods used to build an object. Any code you want to occur when the object first appears should go in the constructor. Most often you use the constructor to initialize your properties so I do that for the Critter class function construct handle anonymous this- name handle end constructor To specify that a function is a class constructor it should be called construct. That s construct preceded by two underscores. 255 The_construct name for constructors was added in PHP . If you have an earlier version of PHP the constructor will have the same name as the class but is still a function defined within the class. The constructor is often .