You are on page 1of 3

Chapter 6.

CLASSES AND OBJECTS

Defining a Class

Creating an Object

Accessing Properties and Methods

Object-oriented programming was devised as a solution to problems associated with large

software projects where many programmers work on a single system. When source code

grows to be tens of thousands of lines of code or more, each change can cause

unexpected side effects. This happens when modules form secret alliances like nations in

pre-WWI Europe. Imagine a module for handling logins that allows a credit card

processing module to share its database connection. Surely it was done with the best

intentions, probably to save the overhead of acquiring another connection. Some time

later, the login module severs the agreement by changing the variable name. The credit

card processing code breaks; then the module that handles invoices breaks. Soon totally

unrelated modules are dragged into the fray.

So, I'm being a bit dramatic. Most programmers pick up an appreciation for coupling and

encapsulation. Coupling is the measure of how dependent two modules are. Less

coupling is better. We'd like to take modules from existing projects and reuse them in

new projects. We'd like to make wholesale changes to the internals of modules without

worrying about how they affect other modules. The solution is to follow the principle of

encapsulation. Modules are treated as independent states, and exchanges between

modules are done through narrow, structured interfaces. Modules do not spy on each

other by reaching into each other's variables. They ask politely through functions.

Encapsulation is a principle you can apply in any programming language, if you have

discipline. In PHP, and many procedural languages, it's easy to be tempted to be lazy.

Nothing prevents you from building a web of conceit between your modules. Object
oriented programming is a way of making it nearly impossible to violate encapsulation.

In object-oriented programming, modules are organized into objects. These objects have

methods and properties. From an abstract perspective, methods are things an object does,

and properties are the characteristics of the object. From a programming perspective,

methods are functions and properties are variables. In an ideal object-oriented system,

each part is an object. And the running of the system consists of objects exchanging

objects with other objects using methods.

Each language takes a different approach to objects. PHP borrows from C++ and offers a

data type that may contain functions and variables under a single identifier. When PHP

was first conceived, even when version 3 was created, PHP wasn't intended as capable of

powering projects of 100,000 lines or more of code. Due to recent advances built into

PHP and Zend, this is a reality. But no matter the size of your project, building your

Defining a Class

When you declare a class, you are really making a template for the creation of objects.

You list all the variables the object should have and all the functions it will need.

Sometimes these are called properties and methods, respectively. Figure 6-1 displays the

form of a class declaration. Note that inside the curly braces you can only declare

variables with the var statement or declare functions. Listing 6.1 shows the definition of

a class with three properties and two methods.

Figure 6-1. Defining a class.

When you declare a property, you don't specify a data type. It is a variable like any other,

and it may contain an integer, a string, or even another object. Depending on the

situation, it might be a good idea to add a comment near the declaration of the property

that states its intended use and data type. When you declare a method, you do so just as

you would a function outside a class definition. Both methods and properties exist within
their own scope, or name space. That means you can safely create methods that have the

same name as functions declared outside of class definitions without conflicts. An

exception to this are built-in functions. For example, you cannot have a print method.

Aside from the variables passed as arguments, methods contain a special variable called

this. It stands for the particular instance of the class. You must use this to refer to

properties and other methods of the object. Some object-oriented languages assume an

unqualified variable that refers to a local property, but in PHP any variables referred to

within a method are simply variables local to that scope. Note the use of the this

variable in the constructor for the user class in Listing 6.1.

If you choose to declare a function within a class that has the same name as the class

itself, the function will be considered a constructor and will be executed immediately

upon creating an object from that class. Typically the constructor is used to initialize the

object's properties. Like any other function, the constructor may have parameters and

You might also like