当对象被打印时,如果该类定义了该方法,则打印该方法的返回值,否则将按照php的缺省行为输出打印结果。该方法类似于java中的tostring()。
复制代码
class testclass {
public function __tostring() {
return this is testclass::__tostring.\n;
}
}
$testobj = new testclass();
print $testobj;
复制代码
运行结果如下:
stephens-air:desktop$ php test.php
this is testclass::__tostring.
2. __get和__set:
这两个方法用于处理类中未声明的属性访问。当对象使用者试图访问未声明的对象属性时,__get()会被调用,并带有一个包含要访问的属性名称字符串作为参数。无论从__get()方法返回什么,都会直接返回给调用者,就如同带有该值的属性存在一样。另外需要注意的是,如果属性存在,但是其访问可见性为private或protected,那么这两个拦截方法同样会被调用,反之,如果属性存在切可访问,那么直接访问属性即可,这两个方法将不再会被调用。以下为__get()拦截方法的示例代码:
复制代码
privatefield = this is a private field.\n;
$this->publicfield = this is a public field.\n;
}
public function __get($property) {
print __get() is called.\n;
$method = get${property};
if (method_exists($this, $method)) {
return $this->$method();
}
return this is undefined field.\n;
}
public function getprivatefield() {
return $this->privatefield;
}
}
$testobj = new testclass();
print $testobj->privatefield;
print $testobj->undefinedfield;
print $testobj->publicfield;
复制代码
运行结果如下:
stephens-air:desktop$ php test.php
__get() is called.
this is a private field.
__get() is called.
this is undefined field.
this is a public field.
__set()方法被调用的规则和__get()基本相同,差别是用于拦截未定义或不可见类属性的赋值操作。另外,该方法接收两个参数,分别是属性名称和要设定的值。见如下代码示例:
复制代码
privatefield = this is a private field.\n;
$this->publicfield = this is a public field.\n;
}
public function __get($property) {
print __get() is called.\n;
$method = get${property};
if (method_exists($this, $method)) {
return $this->$method();
}
return this is an undefined field.\n;
}
public function __set($property, $value) {
print __set is called.\n;
$method = set${property};
if (method_exists($this, $method)) {
$this->$method($value);
} else {
print this is an undefined field.\n;
}
}
public function getprivatefield() {
return $this->privatefield;
}
public function setprivatefield($value) {
$this->privatefield = $value;
}
}
$testobj = new testclass();
$testobj->privatefield = this is a private field after set.\n;
$testobj->undefinedfield = this is a undefined field after set.\n;
$testobj->publicfield = this is a public field after set.\n;
print $testobj->privatefield;
print $testobj->undefinedfield;
print $testobj->publicfield;
复制代码
运行结果如下:
复制代码
stephens-air:desktop$ php test.php
__set is called.
__set is called.
this is an undefined field.
__get() is called.
this is a private field after set.
__get() is called.
this is an undefined field.
this is a public field after set.
复制代码
3. __isset和__unset:
这两个拦截方法被调用的规则和__get()和__set()非常类似,只是用于类中不存在或不可见属性被isset()和unset()两个全局方法应用时才会被分别触发。
复制代码
privatefield = defined private field;
$this->publicfield = defined public field;
}
public function __isset($property) {
print __isset is called.\n;
return isset($this->$property);
}
public function __unset($property) {
print __unset is called.\n;
if (isset($this->$property)) {
unset($this->$property);
}
}
}
$testobj = new testclass();
print 'isset($testobj->privatefield) is '.(isset($testobj->privatefield) ? true : false).\n;
print 'isset($testobj->undefinedfield) is '.(isset($testobj->undefinedfield) ? true : false).\n;
print 'isset($testobj->publicfield) is '.(isset($testobj->publicfield) ? true : false).\n;
print after unset......\n;
//下面两个函数调用后,$testobj的两个对象属性均会变为不可用。
//另外从输出结果来看,__unset方法仅仅被调用一次,因为publicfield为可见属性,所以__unset不会因该属性而被调用。
unset($testobj->privatefield);
unset($testobj->publicfield);
print 'isset($testobj->privatefield) is '.(isset($testobj->privatefield) ? true : false).\n;
print 'isset($testobj->publicfield) is '.(isset($testobj->publicfield) ? true : false).\n;
复制代码
运行结果如下:
复制代码
stephens-air:desktop$ php test.php
__isset is called.
isset($testobj->privatefield) is true
__isset is called.
isset($testobj->undefinedfield) is false
isset($testobj->publicfield) is true
after unset......
__unset is called.
__isset is called.
isset($testobj->privatefield) is false
__isset is called.
isset($testobj->publicfield) is false
复制代码
4. __call:
__call()方法是一个非常有用但又非常容易被滥用的拦截方法。当对象使用者试图访问当前对象未定义的成员函数时,__call()会被自动调用,同时传递两个参数,分别为函数名称和传递给调用函数的所有参数(数组)。__call方法返回的任何值都会返回给函数调用者,就如同该成员函数真实存在一样。下面给出一个非常有用的委托示例。
复制代码
delegateobj = new delegateclass();
}
public function __call($method, $args) {
$this->delegateobj->$method($args[0],$args[1]);
}
}
$testobj = new testclass();
$testobj->printmessage(hello,world);
复制代码
运行结果如下:
stephens-air:desktop$ php test.php
delegateclass:delegatedmethod is called.
$arg1 = helloand $arg2 = world
从以上示例可以看出,testclass并未声明printmessage成员方法,但是通过__call()方法的巧妙桥接直接传递给了委托对象。个人认为该技巧为双刃剑,切勿过度使用。
5. 回调函数:
回调函数的应用场景无须多述,在c/c++中充斥着无数的回调函数典型用例。 这里只是简单给出php中回调函数的使用规则。见如下示例代码和关键性注释:
复制代码
name = $name;
$this->price = $price;
}
}
class processsale {
private $callbacks;
function registercallback($cb) {
if (!is_callable($cb)) {
throw new exception(callback not callable.);
}
$this->callbacks[] = $cb;
}
function sale($product) {
print {$product->name}: processing \n;
foreach ($this->callbacks as $cb) {
//以下两种调用方式均可。
call_user_func($cb, $product);
$cb($product);
}
}
}
$logger = function($product) {
print logging ({$product->name})\n;
};
$processor = new processsale();
$processor->registercallback($logger);
$processor->sale(new product(shoes,6));
print \n;
$processor->sale(new product(coffee,6));
复制代码
运行结果如下:
复制代码
stephens-air:desktop$ php test.php
shoes: processing
logging (shoes)
logging (shoes)
coffee: processing
logging (coffee)
logging (coffee)
复制代码
6. use(闭包):
在javascript中存在大量的闭包应用,php中的闭包则是通过use关键字来完成的。对于闭包这个概念本身而言,简要的说就是函数内的代码可以访问其父作用域中的变量。见如下示例代码和关键性注释:
复制代码
name = $name;
$this->price = $price;
}
}
class processsale {
private $callbacks;
function registercallback($cb) {
if (!is_callable($cb)) {
throw new exception(callback not callable.);
}
$this->callbacks[] = $cb;
}
function sale($product) {
print {$product->name}: processing \n;
foreach ($this->callbacks as $cb) {
$cb($product);
}
}
}
class totalizer {
static function warnamount($amt) {
$count = 0;
//注意这里的$amt和$count均为闭包变量,其中&$count是以引用的形式传递的,即一旦函数内部修改了该变量的值,
//那么下次再访问该闭包变量时,$count将为之前调用中修改后的值。
return function($product) use($amt, &$count) {
$count += $product->price;
print count: $count\n;
if ($count > $amt) {
print high price reached: {$count}\n;
}
};
}
}
$processor = new processsale();
$processor->registercallback(totalizer::warnamount(8));
$processor->sale(new product(shoes,6));
$processor->sale(new product(coffee,6));
复制代码
运行结果如下:
shoes: processing
count: 6
coffee: processing
count: 12
high price reached: 12
注:该blog中记录的知识点,是在我学习php的过程中,遇到的一些php和其他面向对象语言相比比较独特的地方,或者是对我本人而言确实需要簿记下来以备后查的知识点。虽然谈不上什么深度,但是还是希望能与大家分享。
http://www.bkjia.com/phpjc/635016.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/635016.htmltecharticle1. __tostring: 当对象被打印时,如果该类定义了该方法,则打印该方法的返回值,否则将按照php的缺省行为输出打印结果。该方法类似于java中...