關於Laravel中的後期靜態繫結

來源:互聯網
上載者:User
本篇文章主要介紹了淺談Laravel中的一個後期靜態繫結,內容挺不錯的,現在分享給大家,也給大家做個參考。

關於 PHP 的 new static 延遲靜態繫結,或者叫後期靜態繫結,在 Laravel 中遇到一個使用上的問題。如下,在 Laravel 中調用 Model 新增資料的時候,首先給 Model 加了一個擷取分表的方法:

protected function addToMessage($msgType, $userID, $commentID, $replyCommentID, $replyUserID, $gameID){  if (!$userID) {    return false;  }   $table = 't_message_' . hashID($userID, 100);  $this->message->setTable($table)->create([    'msg_type'     => $msgType,    'user_id'     => $userID,    'comment_id'    => $commentID,    'reply_comment_id' => $replyCommentID,    'reply_user_id'  => $replyUserID,    'game_id'     => $gameID,    'is_read'     => 0,    'created_at'    => date('Y-m-d H:i:s'),  ]);  return true;}

這裡 setTable 方法是在 Model 裡定義的擷取分表的方法:

public function setTable($table){  $this->table = $table;  return $this;}

從報錯日誌中發現 $this->table 並沒有生效,但實際上在調用 create 方法之前列印表名的時候是期望的值,這裡調用 create 方法為什麼 $this->table 沒有被重設呢?

這裡 $this->message 是一個繼承 Model 類的模型類,其中 create 方法:

public static function create(array $attributes = []){  $model = new static($attributes);   $model->save();   return $model;}

位於 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php Line 557.

因為 Laravel 架構的這個 Model 類是一個 abstract 類型,PHP 中 abstract 類可以用 new static 後期靜態繫結的方式執行個體化,而 create 方法裡 $model = new static($attributes) 實際上就是重新執行個體化了並返回,而調用者 Model 類沒有定義 table 屬性,所以這個時候 $this->table 是沒有值的。

解決辦法是用 save 方法即可,。實際上 create 方法也調用了 save 方法。

實驗

一個抽象類別 A,有個 create 方法,通過延遲靜態繫結執行個體化並返回。B 類繼承 A,test 方法中修改父類的 name 屬性。

<?php abstract class A{  protected $name = "tanteng";   public static function create()  {    return new static();  }} class B extends A{  //protected $name = '紙牌屋弗蘭克';   public function test()  {    $this->name = "Tony Tan";    return $this;  }} $obj1 = (new B)->test();$obj2 = (new B)->test()->create();var_dump($obj1);var_dump($obj2);

結果顯示 $obj1 和 $obj2 這兩個執行個體都是 B 的執行個體,調用 test 方法屬性 name 改變了,但是調用 create 方法後,name 屬性並沒有改變。 這也就是在本文中說的在 Lavarel 中遇到的情境。 (這裡如果把注釋開啟,列印的 name 就是重寫的值)

如果把抽象類別 A 改成普通類,new static 改成 new self 的方式執行個體化,結果就不同了,列印的屬性 name 都是各自類的屬性。

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.