深入php面向对象、模式与实践

需要操作原对象,但又不想影响原对象.

复制代码 代码如下:


$K_back = clone $K;

基本数据类型和数组都为真复制,即为真副本,当属性为对象时,为假复制,改变副本仍会影响原对象.解决方案:

//在原对象中添加 function __clone(){ $this->对象 = clone $this->对象 }

__clone在clone前自动触发,可以执行一些在备份前的属性操作.

2、&传递引用

方法引用传递,改变源对象

复制代码 代码如下:


function set_K(& $K){...}
function & get_K(){...}

3、static延迟静态绑定

应用场景:Dog类和Person类都需要一个返回实例化的方法,Dog类和Person类都继承于Animal抽象类.

abstract class Animal{ public static function create(){ //实例化调用类 return new static(); } } class Person extends Animal{...} //返回Person实例化类 Person::create();

4、拦截器

__get($property),访问未定义的属性时调用.
__set($property,$value),给未定义的属性赋值时被调用.
__isset($property),对未定义属性调用isset()方法时调用.
__unset($property),对未定义属性调用unset()方法时调用.
__call($method,$arg_array),调用未定义方法时调用.
__call很有用,但要慎用,因为太灵活.
应用场景:有一个专门打印Person类信息的Person_Writer类,如果通过Person类调用Person_Writer类.

//Person委托Person_Writer类处理打印事务. class Person { private $writer; ... function __call($method_name,$args){ if(methood_exists($this->wirter,$method_name)){ return $this->writer->method_name($this); } } //高级__call写法,当委托方法参数不确定时使用. function __call($method_name,$args){ //当然这里这样写法意义不大,但是call一般都是用call_user_func_array调用 $args = $this ; if(methood_exists($this->wirter,$method_name)){ return call_user_func_array( array($this->writer,$method_name),$args); ) } } }

5、回调函数

应用场景: 3个类,Product类,Product_Sale类,Product_Totalizer类,要实现:当卖出Product总共价格超过指定金额时,输出警告.

//Product class Product { public $name; public $price; } //Product_Sale class Product_Sale { private $callbacks; //记录回调函数 function register_callback ($callback) { if(! is_callback($callback)){ thow new Exception('callback not callable'); } $this->callbacks[] = $callback; } //执行回调函数 function sale ($product){ print "{$product->name} : 处理中 \n"; foreach($this->callbacks as $callback){ call_user_func($callback , $product); } } } //Produce_Totalizer class Produce_Totalizer { static function warn_amount ($amt) { $count = 0; return function ($produce) use ($amt , &count) { $count += $produce->price; print " count : {count}\n" if($count>$amt){ print "超过指定金额{$amt}啦~"; } }; } } //模拟场景 $product_sale = new Produce_Sale(); //指定报警金额为8块 $product_sale = register_callback(Produce_Totalizer::warn_amount(8)); //卖商品 $product_sale->sale(new Product("Durex",6)); $product_sale->sale(new Produce("Jissbon",5)); //输出结果 Durex : 处理中 count :6 Jissbon : 处理中 count: 11 超过指定金额8块啦~

6、get_class()和instanceof

get_class(类)用于判断是否精准等于类名;

instanceof 可以判断是否其本身或继承于某父类.

7、类中的方法和类中的属性

复制代码 代码如下:


get_class_methods('类名'):获取类中所有方法.
get_class_vars('类名'):获取类中所有public参数;

8、反射API

2 模式

2.1 组合

问题:课堂类被演讲类和研讨会类继承着.但是演讲类和研讨类都要实现一次性计费和上N次课计费的方法.和输出计算的方式.

解决方案1: 在课堂类中添加计算一次性付费的方法,上N次课的计费方法和输出计算方式的方法.

解决方案2: 运用组合,将处理计费和输出计算方式单独封装为一个计费策略类.

组合模式

abstract class Cost_Strategy { protected $duration; abstract function cost (); abstract function charge_type(); public __construct($duration){ $this->duration = $duration; } } class Timed_Const_Strategy extends Cost_Stratedy { function cost () { //上一次课给5块钱- -. return $this->duration * 5; } function charge_type(){ return "多次课结算"; } } class Fixed_Const_Strategy extends Cost_Stratedy { function cost (){ return 30 ; } function charge_type(){ return "一次性课结算"; } } abstract class Leason { private $cost_strategy; public __construct(Const_Strategy $cost_strategy){ $this->cost_strategy = $cost_strategy; } function __call($method_name,$args){ $args = $cost_strategy ; if(methood_exists($this->cost_strategy,$method_name)){ return call_user_func_array( array($this->writer,$method_name),$args); ) } } } //运用 $leasons[] = new Seminar(new Timed_Const_Strategy(4)); $leasons[] = new Lecture(new Fixed_Const_Strategy(null)); foreach ($leasons as $leason){ print "leason charge : {$leason->const()}"; print "charge_type : {$leason->charge_type()}" } leason charge 20. charge_type : 多次课结算; leason charge 30. charge_type : 一次课结算;

组合既委托.同级委托.

继承既父子关系.

3 生成对象
3.1 单例模式

确保系统中只有唯一一个用例.例如系统配置文件.

重点

1: 构造方法私有.

2: 类本身包含自己的实例化属性.

单例模式

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwyjz.html