久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      直擊php反射學(xué)習(xí)之不用new方法實例化類操作

      直擊php反射學(xué)習(xí)之不用new方法實例化類操作

      本文實例講述了php反射學(xué)習(xí)之不用new方法實例化類操作。分享給大家供大家參考,具體如下:

      上一篇php反射入門示例簡單介紹了 php 反射的幾個常見類的使用方法,但是用反射能做些什么,你可能還是想象不到,

      下面我稍微應(yīng)用反射類來做點東西,大家知道實例化一個類需要用new 關(guān)鍵字,不用 new 可以嗎?答案是可以的,用反射就能實現(xiàn):

      相關(guān)學(xué)習(xí)推薦:PHP編程從入門到精通

      首先創(chuàng)建一個文件 student.php:

      <?php class Student {   public $id;   public $name;   public function __construct($id,$name)   {     $this->id = $id;     $this->name = $name;   }   public function study()   {     echo $this->name.' is learning.....'.PHP_EOL;   }   public function showBag(){     echo "My bag have ".$this->bag->all();   } }

      另新建一個文件run.php

      <?php require 'student.php'; function make($class, $vars = []) {   $ref = new ReflectionClass($class);   if(!$ref->isInstantiable()) {     throw new Exception("類{$class} 不存在");   }   $constructor = $ref->getConstructor();   if(is_null($constructor)) {     return new $class;   }   $params = $constructor->getParameters();   $resolveParams = [];   foreach ($params as $key=>$value) {     $name = $value->getName();     if(isset($vars[$name])) {       $resolveParams[] = $vars[$name];     } else {       $default = $value->isDefaultValueAvailable() ? $value->getDefaultValue() : null;       if(is_null($default)) {         if($value->getClass()) {           $resolveParams[] = make($value->getClass()->getName(), $vars);         } else {           throw new Exception("{$name} 沒有傳值且沒有默認值。");         }       } else {         $resolveParams[] = $default;       }     }   }   return $ref->newInstanceArgs($resolveParams); }

      run.php 中make 函數(shù)就是我們用來實例化類而編寫的函數(shù),第一個參數(shù)傳入類名,第二個參數(shù)是類的構(gòu)造函數(shù)需要傳入的參數(shù)數(shù)據(jù)。

      根據(jù) Student 的構(gòu)造函數(shù)的參數(shù)不同有幾種情況:(以下代碼,請按不同情況追加到 run.php 中運行)

      情況一: 沒有提供 $name 的值

      try {   $stu = make('Student', ['id' => 1]);   print_r($stu);   $stu->study(); } catch (Exception $e) {   echo $e->getMessage(); }

      在構(gòu)造函數(shù)中$name 沒有默認值時,情況一會報錯, 你可以稍微修改下 Student類,給 $name 提供一個默認值,這時候就不會報錯了。

      情況二 提供了 $name 的值

      try {   $stu = make('Student', ['id' => 1, 'name' => 'li']);   print_r($stu);   $stu->study(); } catch (Exception $e) {   echo $e->getMessage(); }

      情況三,我們把 student.php 改一下

      <?php class Bag{   public function name(){     return "學(xué)生包".PHP_EOL;   } } class Student {   public $id;   public $name;   public function __construct($id, $name="xxx", Bag $bag)   {     $this->id = $id;     $this->name = $name;     $this->bag = $bag;   }   public function study()   {     echo $this->name.' is learning.....'.PHP_EOL;   }   public function showBag(){     echo "My bag is ".$this->bag->name();   } }

      可以看到,給 Student 類加了一個參數(shù)$bag, 類型 是 Bag

      這時候運行一下

      <?php try {   $stu = make('Student', ['id' => 1, 'name' => 'li']);   print_r($stu);   $stu->study();   $stu->showBag(); } catch (Exception $e) {   echo $e->getMessage(); }

      可以看到構(gòu)造函數(shù)的第三個參數(shù) $bag ,被自動實例化了,然后傳遞給了 Student 類的構(gòu)造函數(shù),這個部分很關(guān)鍵,這個地方可以用來實現(xiàn)依賴注入,我們不必在手動實例化對象了,我們可以根據(jù)參數(shù)的對應(yīng)的類來自動實例化對象,從而實現(xiàn)類之間的解耦。如果你學(xué)過 Laravel的話,你應(yīng)該對這個很熟悉了。

      贊(0)
      分享到: 更多 (0)
      ?
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號