Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

Introduction to Object-Oriented Programming

The PHP programs you have written so far have mostly been self-

contained—that is, most elements of the code, such as variables,

statements, and functions, exist within a script section. For example,

you might create a Web page for an online retailer that uses PHP to

calculate the total for a sales order, including state sales tax and ship-

ping. However, suppose the retailer sells different types of products

on different Web pages, with one page selling apparel, another page

selling electronics, and so on. If you want to reuse the code that cal-

culates sales totals on multiple Web pages, you must copy all of the

statements or recreate them from scratch for each Web page. Object-

oriented programming takes a different approach. Essentially, object-

oriented programming allows you to use and create objects, which

are complex data structures built of variables and functions that work

together to represent a single entity. In other words, object-oriented

programming allows you to hide a complex logical construct behind a

simple interface.

PHP 5 added many new object-oriented programming capabilities to

the language. These capabilities rival features in other object-oriented

languages, such as Java and C++. Entire books are written about

object-oriented programming, but this chapter focuses on the basics

to get you started in creating object-oriented PHP scripts.

557

Introduction to Object-Oriented

Programming

The term object-oriented programming (OOP) refers to the concept

of merging related variables and functions into a single interface. The

term object specifically refers to programming code and data that

can be treated as an individual unit or component. (Objects are often

also called components.) For example, you might create a Loan object

that calculates the number of payments required to pay off a loan.

The Loan object might also store information such as the principal

loan amount and the interest rate. The term data refers to informa-

tion contained within variables or other types of storage structures.

The functions associated with an object are called methods, and the

Variables associated with an object are called properties or attributes.

In the Loan object example, a function that calculates the number of

payments required to pay off the loan is a method. The principal loan

amount and the interest rate are properties of the Loan object.

Objects can range from simple controls, such as a button, to entire

programs, such as a database application. Some programs consist

entirely of other objects. You’ll often encounter objects that have been

designed to perform a specific task. For example, in a retail sales pro-

gram, you could refer to all of the code that calculates the sales total

CHAPTER 10

Developing Object-Oriented PHP

as a single object. You could then reuse that object repeatedly within

the same program or in others.

C++, Java, and Visual Basic are some popular object-oriented program-

ming languages. Programmers can use any of these languages to cre-

ate objects themselves or use objects created by other programmers.

Often, objects are packaged into libraries, which can be used by other

programs built for the same operating system. For example, if you are

creating an accounting program in Visual Basic, you can use an object

named Payroll that is in a library created in C++. The Payroll object

might contain one method that calculates the amount of federal and

state tax to deduct, another function that calculates the FICA amount

to deduct, and so on. Properties of the Payroll object might include

an employee’s number of tax withholding allowances, federal and state

tax percentages, and the cost of insurance premiums. You do not need

to know how the Payroll object was created in C++, nor do you need

to recreate it in Visual Basic. You only need to know how to access the

methods and properties of the Payroll object from Visual Basic.

A simple object-oriented accounting program is illustrated in Figure

10-1. In this figure, the accounting program is composed of three

separate objects, or components: an AccountsReceivable object, a

Payroll object, and an AccountsPayable object. It is important to

understand that you do not need to rewrite these three objects for the

accounting program; the program only needs to call their methods

and provide the correct data to their properties.

558

The diagram

in Figure 10-1

was created in

Unified

Modeling

Language (UML), a lan-

guage that uses symbols

to represent software

elements such as

objects, methods, and

properties. UML is useful

for designing and docu-

menting software and

other types of engineer-

ing systems.

Figure 10-1

Accounting program and components


Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]