Home > PHP Framework > ThinkPHP > Detailed example of c() method of ThinkPHP framework

Detailed example of c() method of ThinkPHP framework

PHPz
Release: 2023-04-11 11:28:47
Original
937 people have browsed it

With the continuous development of the Internet, the Web development framework is constantly updated and improved in line with the development trend. Among them, many PHP developers tend to use the ThinkPHP framework, especially the ThinkPHP 5 version, which pays special attention to ease of use and flexibility and has won widespread favor among the developer community. Among them, the c() method of the ThinkPHP framework is one of its core features. This article will introduce the c() method of the ThinkPHP framework in detail.

1. Overview of the c() method

The c() method is a very commonly used core method in ThinkPHP 5, which is used to instantiate classes. In simple terms, this method simply creates an object so that we can call methods and properties in the object. The basic usage is as follows:

$c = new \app\index\controller\Test;  // 创建Test类对象
$c->test(); // 调用Test类中的test()方法
Copy after login

However, using the c() method can make the code more concise and convenient:

$c = \think\Controller::class;  // 获取Think\Controller类
$instance = $c::getInstance(); // 创建Think\Controller对象
$instance->assign('name', $name); // 调用Think\Controller对象的assign()方法
Copy after login

By using the c() method, we can complete the example in a few lines of code ization and method calling operations, greatly improving coding efficiency.

2. Detailed use of c() method

  1. Basic use

The most basic use of c() method is for instantiation of classes , and it also supports automatic dependency injection. Specifically, we can use it like this:

// 创建对象
$instance = c(\app\index\controller\Test::class);

// 自动注入依赖
$instance = c(\app\index\controller\Test::class, [$arg1, $arg2, $arg3]);
Copy after login

Among them, the first parameter indicates the name of the class that needs to be instantiated, and the second parameter is an optional parameter, indicating the parameter list required by the constructor of the class. If the second argument is omitted, it defaults to the default constructor.

  1. Support containers

In ThinkPHP 5, containers are a very important concept. I won’t go into details here. We mainly introduce how to use c() in containers. method.

First, we need to bind the class to the container. This can be done in the service provider. We try to use it like this:

namespace app\index\provider;

use think\Service;
use app\index\controller\Test;

class AppServiceProvider extends Service
{
    public function register()
    {
        // 绑定Test类到容器中
        $this->app->bind(Test::class, function(){
            return new Test();
        });
    }
}
Copy after login

In this way, in the container, we can directly use the c() method to instantiate the Test class:

$instance = c(Test::class);
Copy after login
  1. Supports calling static methods and static properties

In addition to supporting the instantiation of classes, the c() method can also be used to call static methods and static properties. The specific usage is as follows:

// 调用静态方法
$result = c(\app\index\controller\Test::class . '::testStatic');

// 调用静态属性
$result = c(\app\index\controller\Test::class . '::$name');
Copy after login

Among them, the colon (::) means calling a static method or static property.

  1. Uppercase c() method

Sometimes, we can also use the uppercase C() method to instantiate a class. This method will convert the first letter of the class name into uppercase, for example:

$instance = C('app\index\controller\Test');
Copy after login

This method can also be used to call static properties and static methods, the method is the same as the lowercase c() method.

3. Summary

The c() method is a core feature of the ThinkPHP framework. It can simplify the process of object instantiation and method calling and improve coding efficiency. We can use basic usage to instantiate classes and perform dependency injection, or we can use it to instantiate and call objects in containers. In addition, we can also use it to call static properties and static methods, which greatly simplifies the code. In short, the c() method is a very practical method that can greatly improve efficiency in our development process.

The above is the detailed content of Detailed example of c() method of ThinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template