The new static delay static binding for PHP, or late static binding, encountered a problem with use in Laravel. This paper mainly introduces a brief discussion on the static binding in the late Laravel, small series feel very good, and now share to everyone, but also to make a reference. Follow the small series together to see it, hope to help everyone.
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.