Peter Lavin
This chapter is excerpted from Object-Oriented PHP: Concepts, Techniques, and Code from NoStarch Press.
PHP 3 was released in mid-1998. Some basic object-oriented (OO) capabilities were included, more or less as an afterthought, to “provide new ways of accessing arrays.”1 No significant changes were made to the object model when version 4 was released in mid-2000. The basics of object-oriented programming (OOP) were there–you could create a class and single inheritance was supported.
With the release of PHP 5 in 2004 there was plenty of room for improving PHP’s OO capabilities. At this point, Java, the most popular OO language
to date, had already been around for almost 10 years. Why did it take PHP so
long to become a full-fledged OO language? The short answer is because
PHP is principally a web development language and the pressures of web
development have only recently pushed it in this direction.
to date, had already been around for almost 10 years. Why did it take PHP so
long to become a full-fledged OO language? The short answer is because
PHP is principally a web development language and the pressures of web
development have only recently pushed it in this direction.
1 See Zeev Suraski, “Object-Oriented Evolution of PHP,” available at www.devx.com/webdev/.
Support for objects has been grafted onto the language–you can choose to use objects or simply revert to procedural programming. That PHP is a
hybrid language should be viewed as something positive, not as a disadvantage.
There are some situations where you will simply want to insert a snippet
of PHP and other situations where you will want to make use of its OO
capabilities.
hybrid language should be viewed as something positive, not as a disadvantage.
There are some situations where you will simply want to insert a snippet
of PHP and other situations where you will want to make use of its OO
capabilities.
As I have already argued in Chapter 1, in some cases, an OO solution is the only solution. PHP 5 recognizes this fact and incorporates a full-blown
object model, consolidating PHP’s position as the top server-side scripting
language.
object model, consolidating PHP’s position as the top server-side scripting
language.
Like Chapter 2, this will be a chapter of broad strokes. I’ll give a general
overview of how the object model has been improved, and then I’ll get into
the details using concrete examples in later chapters. I’ll also address the
issue of backward compatibility.
overview of how the object model has been improved, and then I’ll get into
the details using concrete examples in later chapters. I’ll also address the
issue of backward compatibility.
Access Modifiers
Chapter 2 identified access modifiers as an essential element of an OO language. PHP 5 gives us everything we would expect in this area. In previous
versions of PHP there was no support for data protection, meaning that all
elements of a class were publicly accessible. This lack of access modifiers was probably the biggest disincentive to using objects in PHP 4.
versions of PHP there was no support for data protection, meaning that all
elements of a class were publicly accessible. This lack of access modifiers was probably the biggest disincentive to using objects in PHP 4.
NOTE
A notion closely related to data protection is information hiding. Access modifiers make information hiding possible by exposing an interface (as defined in Chapter 2). This is also referred to as encapsulation of an object.
Built-in Classes
Every OOP language comes with some built-in classes, and PHP is no exception. PHP 5 introduces the Standard PHP Library (SPL), which provides a
number of ready-made classes and interfaces. As of version 5.1, depending
upon how PHP is configured, all in all, there are well over 100 built-in classes and interfaces–a healthy increase from the number available in version 5.0.
number of ready-made classes and interfaces. As of version 5.1, depending
upon how PHP is configured, all in all, there are well over 100 built-in classes and interfaces–a healthy increase from the number available in version 5.0.
Every OOP language comes with some built-in classes, and PHP is no exception. PHP 5 introduces the Standard PHP Library (SPL), which provides a
number of ready-made classes and interfaces. As of version 5.1, depending
upon how PHP is configured, all in all, there are well over 100 built-in classes and interfaces–a healthy increase from the number available in version 5.0.
number of ready-made classes and interfaces. As of version 5.1, depending
upon how PHP is configured, all in all, there are well over 100 built-in classes and interfaces–a healthy increase from the number available in version 5.0.
Having ready-made objects speeds up development, and native classes
written in C offer significant performance advantages. Even if these built-in
classes don’t do exactly what you want, they can easily be extended to suit
your needs.
written in C offer significant performance advantages. Even if these built-in
classes don’t do exactly what you want, they can easily be extended to suit
your needs.
NOTE
There are far too many classes for us to deal with all of them in this book, and some are still not very well documented. We’ll focus on the classes that are especially noteworthy.
Exceptions
All OOP languages support exceptions, which are the OO way of handling errors. In order to use exceptions, we need the keywords try, catch, and throw. A try block encloses code that may cause an error. If an error occurs, it is 12 thrown and caught by a catch block. The advantage of exceptions over errors is that exceptions can be handled centrally, making for much cleaner code.
Exceptions also significantly reduce the amount of error-trapping code you need to write, which offers welcome relief from an uninspiring task. Also, hav-
ing a built-in exception class makes it very easy to create your own customized exceptions through inheritance.
ing a built-in exception class makes it very easy to create your own customized exceptions through inheritance.
Database Classes
Because PHP is all about building dynamic web pages, database support is all-important. PHP 5 introduces the mysqli (MySQL Improved) extension with
support for the features of MySQL databases versions 4.1 and higher. You
can now use features such as prepared statements with MySQL, and you can
do so using the built-in OO interface. In fact, anything you can do procedur-
ally can also be done with this interface.
support for the features of MySQL databases versions 4.1 and higher. You
can now use features such as prepared statements with MySQL, and you can
do so using the built-in OO interface. In fact, anything you can do procedur-
ally can also be done with this interface.
SQLite is a database engine that is incorporated directly into PHP. It is not a general-purpose database like MySQL, but it is an ideal solution in
some situations, in many cases producing faster, leaner, and more versatile
applications. Again an entirely OO interface is provided.
some situations, in many cases producing faster, leaner, and more versatile
applications. Again an entirely OO interface is provided.
PHP versions 5.1 and higher also bundle PHP Data Objects (PDO) with
the main PHP distribution. If you need to communicate with several differ-
ent database back ends, then this package is the ideal solution. PDO’s
common interface for different database systems is only made possible by
the new object model.
the main PHP distribution. If you need to communicate with several differ-
ent database back ends, then this package is the ideal solution. PDO’s
common interface for different database systems is only made possible by
the new object model.
Given the importance of databases, we’ll deal with them extensively in this book. We’ll develop a MySQL database class starting with Chapter 9.
In Chapter 15 we’ll look at SQLite, and in Chapter 16 we’ll discuss PDO.
In Chapter 15 we’ll look at SQLite, and in Chapter 16 we’ll discuss PDO.
Web Services
In PHP 5 all Extensible Markup Language (XML) support is provided by
the libxml2 XML toolkit (www.xmlsoft.org). The underlying code for the
Simple API for XML (SAX) and for the Document Object Model (DOM)
has been rewritten, and DOM support has been brought in line with the
standard defined by the World Wide Web Consortium.
the libxml2 XML toolkit (www.xmlsoft.org). The underlying code for the
Simple API for XML (SAX) and for the Document Object Model (DOM)
has been rewritten, and DOM support has been brought in line with the
standard defined by the World Wide Web Consortium.
Unified treatment of XML under libxml2 makes for a more efficient and
easily maintained implementation. This is particularly important because sup-
port for XML under PHP 4 is weak, and web services present many problems
that require an OO approach.
easily maintained implementation. This is particularly important because sup-
port for XML under PHP 4 is weak, and web services present many problems
that require an OO approach.
Under PHP 4, creating a SOAP client and reading an RSS feed are
challenging programming tasks that require creating your own classes or
making use of external classes such as NuSOAP (http://sourceforge.net/
projects/nusoap). There’s no such need in PHP 5. In Chapter 12, you’ll
see just how easy these tasks are using the built-in SOAPClient class and
SimpleXMLElement. Again it’s the improved object model that makes this
possible.
challenging programming tasks that require creating your own classes or
making use of external classes such as NuSOAP (http://sourceforge.net/
projects/nusoap). There’s no such need in PHP 5. In Chapter 12, you’ll
see just how easy these tasks are using the built-in SOAPClient class and
SimpleXMLElement. Again it’s the improved object model that makes this
possible.
Reflection Classes
The reflection classes included in PHP 5 provide ways to introspect objects
and reverse engineer code. The average web developer might be tempted
to ignore these classes, but Chapter 14 shows how useful they are for auto-
mating a task that most developers approach with little enthusiasm: the
creation of documentation.
and reverse engineer code. The average web developer might be tempted
to ignore these classes, but Chapter 14 shows how useful they are for auto-
mating a task that most developers approach with little enthusiasm: the
creation of documentation.