Chapter 4. Introduction to Object-Oriented Programming with PHP 5
Object-oriented programming is a several-decades-old concept that has spread to almost every aspect of modern programming languages and practices. The reason is clear as soon as you start to use a convenience such as the powerful PHP-related packages. We introduce PEAR packages in Chapter 7; many operate by defining objects, providing a wealth of useful features in a simple form. You should understand the basics of object-oriented programming in order to make use of packages and an error-recovery feature called exceptions. You may also find object-oriented programming a useful practice in your own code. We'll give you an introduction in this chapter, and present some advanced features in Chapter 14.
While many of the concepts and techniques presented in this chapter work in PHP 4, support is greatly enhanced in PHP 5. In this chapter we describe what you can and can't do in each version of PHP.
Classes and Objects
The basic idea of object-oriented programming is to bind data and
functions in convenient containers called objects . For instance, in Chapter 7 we'll show you how to
standardize the look of your own web pages through an object called a
template. Your PHP code can refer to this object through a variable;
we'll assume here you've decided to call the variable $template. All the complex implementation of
templates is hidden: you just load in the proper package and issue a PHP
statement such as:
$template = new ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access