Brief analysis of Laravel static binding in late stage

Source: Internet
Author: User
This paper mainly introduces a brief discussion on the static binding of laravel in the late period, and share it to everyone to make a reference. We hope to help you.

The new static delay static binding for PHP, or late static binding, encountered a problem with use in Laravel. As below, when the model new data is called in Laravel, the model is first added to the method of getting the table:

protected function Addtomessage ($msgType, $userID, $commentID, $replyCommentID, $replyUserID, $gameID) {  if (!$ UserID) {    return false;  }   $table = ' T_message_ '. Hashid ($userID, +);  $this->message->settable ($table)->create ([    ' msg_type ' +     = $msgType,    ' user_id '     = ' $ ' UserID,    ' comment_id ' and    $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;}

Here the Settable method is defined in Model to obtain the method of the table:

Public Function settable ($table) {  $this->table = $table;  return $this;}

It is found in the error log that the $this->table is not in effect, but in fact it is expected to print the table name before calling the Create method, where the Create method is called $this->table is not reset?

Here $this->message is an inherited model class, where the Create method:

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

Located in vendor\laravel\framework\src\illuminate\database\eloquent\model.php line 557.

Because this Model class of the Laravel framework is an abstract type, the abstract class in PHP can be instantiated in the form of a static late binding of new static, while the Create method $model = new static ($attr Ibutes) is actually re-instantiated and returned, and the caller Model class does not have a table property defined, so at this point $this->table has no value.

The solution is to use the Save method. The Save method is actually called by the Create method.

Experiment

An abstract class A that has a create method that is instantiated and returned by delaying the static binding. Class B inherits the Name property of the parent class in the A,test method.

<?php abstract class a{  protected $name = "Tanteng";   public static function Create ()  {    return new static ()  }} class B extends a{  //protected $name = ' House of Cards Frank '; Public   function test ()  {    $this->name = "Tony Tan";    return $this;  }} $obj 1 = (new B)->test (), $obj 2 = (new B)->test ()->create () Var_dump ($obj 1); Var_dump ($obj 2);

The results show that the two instances of both $obj 1 and $obj 2 are instances of B, and the call to test method property name has changed, but the Name property has not changed since the Create method was called. This is the scenario that was encountered in Lavarel in this article. (If you open the note here, the printed name is the overridden value)

If you change the abstract class A to a normal class, the new static is instantiated in the way of new self, the result is different, and the printed property name is the property of the respective class.

Related recommendations:

Explore how the middleware of Laravel is implemented

Laravel Optimized Split routing file

laravel writing App Interface (API)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.