Home>

php oop coherent operation principle

The->symbol is actually passed an object pointer.Perhaps this is not true.

However, we can understand it this way.

Not much to say. Put the code.

Common usage:

<?php
class test
{
 public $a="";
 public $b="";
 public function actiona () {
  $this->a="hello";
  return $this;
 }
 public function actionb () {
  $this->b="world";
  return $this;
 }
 public function actionc () {
  echo $this->a. "". $this->b;
 }
}
$oktest=new test ();
$oktest->actiona ();
$oktest->actionb ();
$oktest->actionc ();
?>

Consistent usage:

<?php
class test
{
 public $a="";
 public $b="";
 public function actiona () {
  $this->a="hello";
  return $this;
 }
 public function actionb () {
  $this->b="world";
  return $this;
 }
 public function actionc () {
  echo $this->a. "". $this->b;
 }
}
$oktest=new test ();
$oktest->actiona ()->actionb ()->actionc ();
?>

did you see that.

Connected.You can string operations together.

It looks a lot more intuitive.Reading the code is also a lot easier.

Operations in the class all return a pointer.

$this.

It is equivalent to the object you initialized $oktest

So the following operations can be continued.

Try to get rid of each operation

return $this

You will see an error message.

example:

<?php
class sql {
 public $select;
 public $from;
 public $where;
 public $order;
 public $limit;
 public function from ($_ from="from test") {
 $this->from=$_ from;
 return $this;
 }
 public function where ($_ where="where 1=1") {
 $this->where=$_ where;
 return $this;
 }
 public function order ($_ order="order by id desc") {
 $this->order=$_ order;
 return $this;
 }
 public function limit ($_ limit="limit 0,30") {
 $this->limit=$_ limit;
 return $this;
 }
 public function select ($_ select="select *") {
 $this->select=$_ select;
 return $this->select. "". $this->from. "". $this->where. "". $this->order. "". $this->limit;
 }
}
$sql=new sql ();
echo $sql->from ()->where ()->order ()->limit ()->select ();
?>
php
  • Previous Bootstrap tutorial JS plugin popup study note sharing
  • Next C # simple example of calling a Python script