Page 220 - Beginning PHP 5.3
P. 220
Part II: Learning the Language
By the way, you can also use type hinting with regular functions, not just methods, and you can also
check that an argument is an array using hinting:
function showAll( array $items ) {
Sadly, PHP supports type hinting only for objects and arrays. Other data types, such as strings or
integers, can ’ t be checked using this technique. If you want to check for these types you ’ ll need to use
is_string() , is_int() , and so on as described in Chapter 3 .
Making Your Classes Self - Contained with Encapsulation
So far, most of the classes you ’ ve created in this chapter have contained public properties, and outside
code has been able to reach into a class ’ s innards and manipulate its public properties at will. Usually,
this is a bad idea. One of the strengths of OOP is the concept of encapsulation . This means that a class ’ s
internal data should be protected from being directly manipulated from outside, and that the details
of the class ’ s implementation — such as how it stores values or manipulates data — should be hidden
from the outside world. By doing this, you gain two advantages:
❑ You can change your class ’ s implementation details at any time without affecting code that
uses the class
❑ You can trust the state of an object to be valid and to make sense
Generally speaking, all internal properties of a class should be declared private. If outside code needs to
access those variables, it should be done through a public method. This gives your class the opportunity
to validate the changes requested by the outside code and accept or reject them.
For example, if you ’ re building a banking application that handles details of customer accounts, you
might have an Account object with a property called $totalBalance and methods called
makeDeposit() and makeWithdrawal() . The only way to affect the balance should be to make a
withdrawal or a deposit. If the $totalBalance property is implemented as a public property, you could
write outside code that would increase the value of that variable without having to actually make a
deposit. Obviously, this would be bad for the bank.
Instead, you implement this property as a private property and provide a public method called
getTotalBalance() , which returns the value of that private property:
class Account {
private $_totalBalance = 0;
public function makeDeposit( $amount ) {
$this- > _totalBalance += $amount;
}
public function makeWithdrawal( $amount ) {
if ( $amount < $this- > _totalBalance ) {
$this- > _totalBalance -= $amount;
} else {
die( “Insufficient funds < br / > ” );
}
}
182
9/21/09 9:03:36 AM
c08.indd 182 9/21/09 9:03:36 AM
c08.indd 182