PHP如何使用面向对象魔术方法之__call函数

 4636

PHP使用面向对象魔术方法之__call函数:1、当调用一个不可以访问的成员方法时,【__call】魔术方法就会被调用;2、该成员方法不存在以及成员方法是protected或者private时,调用【__call】魔术方法。


PHP如何使用面向对象魔术方法之__call函数


PHP使用面向对象魔术方法之__call函数:

基本介绍:

(1) 当我们调了一个不可以访问的成员方法时,__call魔术方法就会被调用.

(2) 不可以访问的成员方法的是指(1. 该成员方法不存在, 2. 成员方法是protected或者 private)

需求

我们就是希望,在类的外部直接调用不可访问的成员方法(private , protected).

案例说明

  1. <?php
  2.     header('content-type:text/html;charset=utf-8');
  3.     //__call魔术方法
  4.     class Monk{
  5.         public $name;
  6.         protected $hobby;
  7.         public function __construct($name, $hobby){
  8.             $this->name = $name;
  9.             $this->hobby = $hobby;
  10.         }
  11.         //输出该对象的信息
  12.         public function showInfo(){
  13.             echo '<br> 名字是 ' . $this->name;
  14.             foreach($this->hobby as $hobby){
  15.                 echo '<br> 爱好有 ' . $hobby;
  16.             }
  17.         }
  18.         //会做算术题, 保护的
  19.         protected function getSum($num1, $num2){
  20.             return $num1 + $num2;
  21.         }
  22.         // 编写这个__call魔术方法, __call 魔术方法会接收到两个参数
  23.         /*
  24.         * @param $method_name 就是函数名
  25.         * @param $parameters 就是参数,类型是array
  26.         */
  27.         public function __call($method_name, $parameters){
  28.             // echo '<br> method_name = ' . $method_name;
  29.             // echo '<br> $parameters <br>';
  30.             // var_dump($parameters);
  31.             //判断 $this 中是否有  $method_name 函数,有就返回true,否则返回false
  32.             if(method_exists($this, $method_name)){
  33.                 return $this->$method_name($parameters[0], $parameters[1]);
  34.             }else{
  35.                 return '没有你要调用的函数';
  36.             }
  37.         }
  38.     }
  39.     $monk = new Monk('济公', array('no1'=>'腾云驾雾', 'no2'=>'喝酒'));
  40.     $monk->showInfo();
  41.     // 当我们直接调用 protected 方法时,就会触发 __call 魔术方法
  42.     echo '<br> 结果是' . $monk->getSum(100, 200);

练习题:

  1. <?php
  2.     header('content-type:text/html;charset=utf-8');
  3.     /*
  4.     练习题: 
  5.     请编写一个Cat类(有 年龄, 名字 二个属性),要求二个属性全部都是public。
  6.     Cat类有一个 方法 jiSuan($n1, $n2, $oper) 可以计算+ - * / 是私有的.
  7.     在类外部,$对象名->play('jiSuan', $n1, $n2, $oper) 得到结果,注意play这个方法,在类中没有定义.
  8.     要求 play 是固定的,如果没有按规则写,则给出相应的错误提示!
  9.     */
  10.     class Cat{
  11.         public $name;
  12.         public $age;
  13.         public function __construct($name, $age){
  14.             $this->name = $name;
  15.             $this->age = $age;
  16.         }
  17.         private function jiSuan($n1, $n2, $oper){
  18.             $res = 0;
  19.             switch($oper){
  20.                 case '+':
  21.                     $res = $n1 + $n2;
  22.                     break;
  23.                 case '-':
  24.                     $res = $n1 - $n2;
  25.                     break;
  26.                 case '*':
  27.                     $res = $n1 * $n2;
  28.                     break;
  29.                 case '/':
  30.                     $res = $n1 / $n2;
  31.                     break;
  32.                 default :
  33.                     echo '你输入的运算符号不对';
  34.             }
  35.             return $res;
  36.         }
  37.         //编写一个__call 魔术方法
  38.         public function __call($method_name, $parameters){
  39.             //先判断是否通过 'play' 调用
  40.             if($method_name == 'play'){
  41.                 //继续
  42.                 if( method_exists($this, $parameters[0]) ){
  43.                     //继续
  44.                     return $this->$parameters[0]($parameters[1], $parameters[2], $parameters[3]);
  45.                 }else{
  46.                     return ' 你调用的 ' . $parameters[0] . ' 不存在';
  47.                 }
  48.             }else{
  49.                 return ' 你调用的方式有问题 ';
  50.             }
  51.         }
  52.     }
  53.     $cat = new Cat('小花猫', 3);
  54.     echo '<br> 运算的结果是 ' . $cat->play('jiSuan', 10, 20, '-');




本文网址:https://www.zztuku.com/detail-7906.html
站长图库 - PHP如何使用面向对象魔术方法之__call函数
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐