Many return $this in Laravel
Really do not understand this is what principle, Baidu after found the following better explanation:
class sum{ private $num1; private $num2; public function num1($n) { $this->num1 =$n; return $this; } public function num2($n) { $this->num2=$n; return $this; } public function sum() { return $this->num1+$this->num2; }}$sum=new sum();$sum->num1(10)->num2(5)->sum();
Three questions:
1.
Since NUM1 and num2 are designed to be methods, why do they have to be private to their properties?
Private $num 1;
Private $num 2;
... num1 ($n)
... num2 ($n)
2.
$this->NUM1 = $n
What is the principle of assigning a value to itself ($num 1) of its own parameter values ($n)? What's the use of it?
3.
Return $this This technique, the utilization rate is too high in the larval, the brain cannot turn over.
May be late at night, too many problems, the brain is very sticky, thank you for the answer, for the sense!
Waking up again, I really can't find out.
Reply content:
Many return $this in Laravel
Really do not understand this is what principle, Baidu after found the following better explanation:
class sum{ private $num1; private $num2; public function num1($n) { $this->num1 =$n; return $this; } public function num2($n) { $this->num2=$n; return $this; } public function sum() { return $this->num1+$this->num2; }}$sum=new sum();$sum->num1(10)->num2(5)->sum();
Three questions:
1.
Since NUM1 and num2 are designed to be methods, why do they have to be private to their properties?
Private $num 1;
Private $num 2;
... num1 ($n)
... num2 ($n)
2.
$this->NUM1 = $n
What is the principle of assigning a value to itself ($num 1) of its own parameter values ($n)? What's the use of it?
3.
Return $this This technique, the utilization rate is too high in the larval, the brain cannot turn over.
May be late at night, too many problems, the brain is very sticky, thank you for the answer, for the sense!
Waking up again, I really can't find out.
Chained operations, specific scenarios like this
/* * SQL statement combination instance class, originating article Web development Note * For learning, non-professional class * */class sql{private $sql =array ("fr Om "=" "," where "=" "," order "=" "," Limit "and" ""; Public function from ($tableName) {$this->sql[' from ']= ' from '. $tableName; return $this; The public function where ($_where= ' 1=1 ') {$this->sql["where"]= "where". $_where; return $this; The Public Function order ($_order= ' ID DESC ') {$this->sql["order"]= "ORDER by". $_order; return $this; } Public Function limit ($_limit= ') {$this->sql["limit"]= "Limit 0,". $_limit; return $this; Public Function Select ($_select= ' * ') {return "select". $_select. " ". (Implode ("", $this->sql)); }} $sql =new SQL (), echo $sql->from ("TestTable")->where ("Id=1")->order ("id DESC")->limit ()->select ( );//Output SELECT * from TestTable WHERE id=1 ORDER by id DESC LIMIT 0,10
1, property design as private is to prevent external directly through the property assignment value to modify its value. Only the public method of exposure is used to assign values.
2, the property of the class is assigned operation.
3, realize ring call. $sum->NUM1 (10) assigns the NUM1 of the $sum object, returns the result after execution is the current $sum object, and then calls the Num2 (5) method to copy the num2, returns the current $sum object, and finally calls the sum method of the $sum object.
Returns this to implement a link-like call that is similar to JavaScript
Give me a chestnut:
$object = new ClassName();$object->method1();$object->method2();$object->method3();
This method wants to write:
$object->method1()->method2()->method3();
Then METHOD1,METHOD2,METHOD2 must return to $object, what is $object in method*? It's $this.
Now you see, method1 inside why there is Method2 method, because Method1 returned to $object Ah!