Home  >  Article  >  PHP Framework  >  How to solve thinkphp new self() error problem

How to solve thinkphp new self() error problem

藏色散人
藏色散人forward
2021-11-23 15:52:351759browse

The followingthinkphp frameworkThe tutorial column will introduce to you how to solve the thinkphp new self() error problem. I hope it will be helpful to friends in need!

Specific problem description:

public function t2()  
{  
  
  $receiver = new self();  
 
  
}

How to solve thinkphp new self() error problem

Framework thinkphp6

Solution:

The error message is already obvious: __construct() is missing parameters. The code you posted does not pass in $app. I have not used TP6, but in a framework like this, the method of obtaining an object is When using a container, it will automatically inject dependencies (that is, automatically instantiate $app). If you use new, dependencies will not be injected.

So when using the container, you can do this: $receiver = invoke('xxx');

When using the new method, you can do this:

$app = new App();
$receiver = new Receiver($app);

For details, please refer to the documentation of TP6: https://www.kancloud.cn/manual/thinkphp6_0/1037489

Recommended: "The latest 10 thinkphp video tutorials"

The above is the detailed content of How to solve thinkphp new self() error problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete