25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

114 ” Object Oriented Programming in <strong>PHP</strong><br />

just blueprints that cannot be used directly—they must be instantiated into objects,<br />

which can then interact with the rest of the application. You can think of classes as<br />

the blueprints for building a car, while objects are, in fact, the cars themselves as<br />

they come out of the production line. Just like a single set of blueprints can be used<br />

to produce an arbitrary number of cars, an individual class can normally be instantiated<br />

into an arbitrary number of objects.<br />

Declaring a Class<br />

The basic declaration of a class is very simple:<br />

class myClass {<br />

}<br />

// Class contents go here<br />

As you have probably guessed, this advises the <strong>PHP</strong> interpreter that you are declaring<br />

a class called myClass whose contents will normally be a combination of constants,<br />

variables and functions (called methods).<br />

Instantiating an Object<br />

Once you have declared a class, you need to instantiate it in order to take advantage<br />

of the functionality it offers. This is done by using the new construct:<br />

$myClassInstance = new myClass();<br />

In <strong>PHP</strong> 5, objects are treated differently from other types of variables. An object is<br />

always passed by reference (in reality, it is passed by handle, but for all practical<br />

purposes there is no difference), rather than by value. For example:<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

$myClassInstance = new myClass();<br />

$copyInstance = $myClassInstance();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!