Examples of ThinkPHP 5.0 Framework Controller Inheriting Base and Custom Classes

  • 2021-10-11 17:51:34
  • OfStack

This article illustrates the example of ThinkPHP 5.0 framework controller inheriting base classes and custom classes. Share it for your reference, as follows:

Inherit the system controller base class:


<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
  public function hello()
  {
    return 'hello,world';
  }
}

Or customize one basic controller class Base:


<?php
namespace app\index\controller;
use think\Controller;
class Base extends Controller
{
}

Some common methods can be defined in the Base controller class (if you are not familiar with the basic knowledge of the class, refer to the class and object section of PHP, which is very clear and will not be further discussed here).

Then apply all of the following controller classes to inherit Base:


<?php
namespace app\index\controller;
use app\index\controller\Base;
class Index extends Base
{
  public function hello()
  {
    return 'hello,world';
  }
}

It is suggested to define a controller base class for application system 1, which is convenient for later extension.

PHP does not support multiple inheritance. If you need to inherit multiple classes, you can use the trait .

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: