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

Declaring Data Members

What Is Information Hiding?

One of the fundamental principles in object-oriented programming

is the concept of information hiding. Information hiding gives an

encapsulated object its black box capabilities so that users of a class

can see only the members of the class that you allow them to see.

Essentially, the principle of information hiding states that class mem-

bers should be hidden when other programmers (sometimes called

clients) do not need to access or know about them. Information hid-

ing helps minimize the amount of information that needs to pass in

and out of an object. Information hiding also reduces the complexity

of the code that clients see, allowing them to concentrate on the task

of integrating an object into their programs. For example, if a client

wants to add a Payroll object to an Accounting program, the client

does not need to know the underlying details of the Payroll object’s

member functions, nor does the client need to modify any local data

members that are used by those functions. The client only needs to

know which of the object’s member functions to call and what data (if

any) needs to be passed to those member functions.

Now consider information hiding on a larger scale. Professionally

developed software packages are distributed in an encapsulated

format, which means that the casual user—or even an advanced

programmer—cannot see the underlying details of how the software

Is developed. Imagine what would happen if Microsoft distributed

Excel without hiding the underlying programming details. There is

no need for users to see these details, because users do not need to

understand how the underlying code performs the various spread-

sheet calculations. Microsoft also has a critical interest in protecting

proprietary information, as do you. The design and sale of software

components is big business. You certainly do not want to spend a

significant amount of time designing an outstanding software com-

ponent, only to have unscrupulous programmers steal the code and

claim it as their own. Of course, you cannot hide all of the underlying

code, or other programmers will never be able to integrate your class

with their applications. But you need to hide most of it.

Information hiding on any scale also prevents other programmers

from accidentally introducing a bug into a program when modifying

a class’s internal workings. Well-intentioned programmers will often

attempt to “improve” your code, no matter how well it is written.

Before you distribute your classes to other programmers, your classes

should be thoroughly tested and bug-free. Other programmers can

thus focus on the more important task of integrating your code into

their programs using the data members and member functions you

designate.

579

CHAPTER 10

Developing Object-Oriented PHP

To enable information hiding in your classes, you must designate

access specifiers for each of your class members. You will learn about

access specifiers next.

Using Access Specifiers

580

The

protected

access

specifier is

used with a

more advanced object-

oriented programming

technique called

inheritance.

Prior to PHP

5, the var

keyword was

used to

declare

class data members. If

you use the var keyword

to declare a data member

in PHP 5, it is created

with public access.

The first step in hiding class information is to set access specifiers for

class members. Access specifiers control a client’s access to indi-

vidual data members and member functions. There are three levels

of access specifiers in PHP: public, private, and protected. In this

chapter, you will study the public and private access specifiers.

The public access specifier allows anyone to call the member func-

tion or to modify and retrieve the value of the data member. The

private access specifier prevents clients from calling member func-

tions or accessing data members, and is one of the key elements in

information hiding. Private access does not restrict a class’s internal

access to its own members; a class’s member function can modify

any private data member or call any private member function. Private

access restricts clients from accessing class members.

You include an access specifier at the beginning of a data member

declaration statement. For example, the following statement declares

a public data member named $Balance in the BankAccount class and

initializes it with a value of 0:

class BankAccount {

public $Balance = 0;

}

It is common

practice to list

public class

members first

to clearly iden-

tify the parts of the class

that can be accessed by

clients.

It is considered good programming practice to always assign an initial

value to a data member when you first declare it. The best way to ini-

tialize a data member is with a constructor function (discussed later

in this chapter). You can also assign simple values to data members

when you first declare them, although an error occurs if you attempt

to use any type of expression to initialize the data member. The pre-

ceding statement is valid because it only assigns a value of 0 to the

$Balance data member. However, the following statement is invalid

because it attempts to use an expression (the addition operation) to

assign a value to the $Balance data member:

class BankAccount {

public $Balance = 1 + 2;

}

Similarly, if you have a data member named $CustomerName in the

BankAccount class, you can assign a simple text string to the data

member as follows:


Declaring Data Members

class BankAccount {

public $CustomerName = "Don Gosselin";

}

In comparison, the following statement is invalid because it attempts

to use an expression (the concatenation operation) to assign a value

to the $CustomerName data member:

class BankAccount {

public $CustomerName = "Don" . " " . "Gosselin";

}

581

Recall that to access a data member, you use member selection nota-

tion. Keep in mind that when you use member selection notation,

you do not include a dollar sign before the data member name. For

example, the following statements assign a new value to the $Balance

data member and then display its value:

$Checking->Balance = 958.20;

printf("<p>Your checking account balance is $%.2f.</p>",

$Checking->Balance);

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