1 / 26
文档名称:

PHP5 Power Programming Oop Design Patterns(Tutorial) Php.pdf

格式:pdf   页数:26
下载后只包含 1 个 PDF 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

PHP5 Power Programming Oop Design Patterns(Tutorial) Php.pdf

上传人:bolee65 2014/3/7 文件大小:0 KB

下载得到文件列表

PHP5 Power Programming Oop Design Patterns(Tutorial) Php.pdf

文档介绍

文档介绍:Gutmans_ch04 Page 85 Thursday, September 23, 2004 2:39 PM
CHAPTER 4
PHP 5 Advanced OOP and Design
Patterns
“I made up the term ‘object-oriented,’ and I can tell you I didn’t
have C++ in mind.”—Alan Kay, OOPSLA ’97
INTRODUCTION
In this chapter, you learn how to use PHP’s more advanced object-oriented
capabilities. When you finish reading this chapter, you will have learned
☞ Overloading capabilities that can be controlled from PHP code
☞ Using design patterns with PHP 5
☞ The new reflection API
OVERLOADING CAPABILITIES
In PHP 5, extensions written in C can overload almost every aspect of
the object syntax. It also allows PHP code to overload a limited subset that is
most often needed. This section covers the overloading abilities that you can
control from your PHP code.
Property and Method Overloading
PHP allows overloading of property access and method calls by implementing
special proxy methods that are invoked if the relevant property or method
doesn’t exist. This gives you a lot of flexibility in intercepting these actions and
defining your own functionality.
You may implement the following method prototypes:
function __get($property)
function __set($property, $value)
function __call($method, $args)
85
Gutmans_ch04 Page 86 Thursday, September 23, 2004 2:39 PM
86 PHP 5 Advanced OOP and Design Patterns Chap. 4
__get is passed the property’s name, and you should return a value.
__set is passed the property’s name and its new value.
__call is passed the method’s name and a numerically indexed array of
the passed arguments starting from 0 for the first argument.
The following example shows how to use the __set and __get functions
(array_key_exists() is covered later in this book; it checks whether a key exists
in the specified array):
class StrictCoordinateClass {
private $arr = array('x' => NULL, 'y' => NULL);
function __get($property)
{
if (array_key_exists($property, $this->ar