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