About chain calls in PHP
天蓬老师
天蓬老师 2017-06-19 09:07:08
0
2
755

I encountered a problem when using PHP chain calls:
There is a class "Site" below:

api = new Api(); return $this->api; } } ?>

There is also a class "Api" located in "class.Api.php":

Use the following code to instantiate:

$site = new Site();

Call the following code again:

$site->api()->auth(); $site->api()->render();

Will PHP repeat require() and create new object API? If so, require() can be replaced by require_once(), but how to make "$site->api()" return the same object? Thanks!

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all (2)
为情所困

Single case mode.

api)) { $this->api = new Api(); } return $this->api; } } ?>

It’s just a simple writing, but it still needs a lot of optimization.

    typecho
    require('class.Api.php'); class Site{ protected $api; public function getApi() { return $this->api; } public function api(){ $this->api = new Api(); } } ?>
    $site = new Site(); $site->api(); $site->getApi()->auth(); $site->getApi()->render();
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!