设为首页 收藏本站
查看: 1244|回复: 0

[经验分享] PHP面向对象中的重要知识点(二)

[复制链接]

尚未签到

发表于 2015-8-23 12:14:29 | 显示全部楼层 |阅读模式
  1. __toString:
      当对象被打印时,如果该类定义了该方法,则打印该方法的返回值,否则将按照PHP的缺省行为输出打印结果。该方法类似于Java中的toString()。



<?php
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()拦截方法的示例代码:



<?php
class TestClass {
private $privateField;
public $publicField;
public function __construct() {
$this->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()基本相同,差别是用于拦截未定义或不可见类属性的赋值操作。另外,该方法接收两个参数,分别是属性名称和要设定的值。见如下代码示例:



<?php
class TestClass {
private $privateField;
public $publicField;
public function __construct() {
$this->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()两个全局方法应用时才会被分别触发。



<?php
class TestClass {
private $privateField;
public $publicField;
public function __construct() {
$this->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方法返回的任何值都会返回给函数调用者,就如同该成员函数真实存在一样。下面给出一个非常有用的委托示例。



<?php
class DelegateClass {
function printMessage($arg1, $arg2) {
print "DelegateClass:delegatedMethod is called.\n";
print '$arg1 = '.$arg1.'and $arg2 = '.$arg2."\n";
}
}
class TestClass {
private $delegateObj;
public function __construct() {
$this->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中回调函数的使用规则。见如下示例代码和关键性注释:



<?php
class Product {
public $name;
public $price;
public function __construct($name, $price) {
$this->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关键字来完成的。对于闭包这个概念本身而言,简要的说就是函数内的代码可以访问其父作用域中的变量。见如下示例代码和关键性注释:



<?php
class Product {
public $name;
public $price;
public function __construct($name, $price) {
$this->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和其他面向对象语言相比比较独特的地方,或者是对我本人而言确实需要簿记下来以备后查的知识点。虽然谈不上什么深度,但是还是希望能与大家分享。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-102954-1-1.html 上篇帖子: PHP闭包(Closure)初探 下篇帖子: PHP+uploadify3.2.1上传插件使用之插件参数介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表