Prev | Current Page 360 | Next

Rob Allen, Nick Lo, and Steven Brown

"Zend Framework in Action"

Let??™s
look at this in use and flesh out the ZFiA_Person class in listing 17.3.
Listing 17.3 A simple class
class ZFiA_Person
{
protected $_firstName; A
protected $_lastName; A
public __construct($firstName, $lastName) B
{
$this->_firstName = $firstName; C
$this->_lastName = $lastName; C
}
public fullName() D
{ D
return $this->_firstName . ' ' . $this->_lastName; D
} D
}
A Variables only visible to class and its children
B Constructor class called on new
C Assign to member variables
D Public function used by consumers of class
17.1.3 Extending classes
Extending classes is extremely powerful and can save you a lot of time by facilitating code reuse. When one
class extends another it will inherit the properties and methods of the class it extends provided they are not
???private???. The class being extended is called the parent and the class that is extending the parent is called the
child. When you extend another class, you are creating an ???is a??? relationship. That is your child class ???is a???
parent class with additional functionality. For example, we could create a ZFiA_Author class that extends
ZFiA_Person which means that an Author is a Person. This is shown in Listing 17.4.
Listing 17.4 Extending ZFiA_Person to create ZFiA_Author
class ZFiA_Author extends ZFiA_Person A
{
protected $_title; 1
public function setTitle($title) 2
{ 2
$this->_title = $title; 2
} 2
}
A extends is used to inherit functionality
Licensed to Menshu You

Pages:
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
Pozycjonowanie Wrocław nocleg lodz pompy ciepła kasta.brene.pl Wczasy nad morzem