Home  >  Article  >  Backend Development  >  Recursion - Can I write custom functions in PHP member methods?

Recursion - Can I write custom functions in PHP member methods?

WBOY
WBOYOriginal
2016-09-06 08:57:10963browse

Can I write custom functions in PHP member methods?
I defined a function in a method, but an error was reported when calling it
This is the method that reported the error
Recursion - Can I write custom functions in PHP member methods?
The error was reported as follows

Recursion - Can I write custom functions in PHP member methods?

But I have another method that also writes functions and performs recursion, but no error is reported.

Recursion - Can I write custom functions in PHP member methods?

This is the interface for calling these two methods

Recursion - Can I write custom functions in PHP member methods?

Why is an error reported? Can anyone explain it or have a good way to deal with it

Reply content:

Can I write custom functions in PHP member methods?
I defined a function in a method, but an error was reported when calling it
This is the method that reported the error
Recursion - Can I write custom functions in PHP member methods?
The error was reported as follows

Recursion - Can I write custom functions in PHP member methods?

But I have another method that also writes functions and performs recursion, but no error is reported.

Recursion - Can I write custom functions in PHP member methods?

This is the interface for calling these two methods

Recursion - Can I write custom functions in PHP member methods?

Why is an error reported? Can anyone explain it or have a good way to deal with it

Why not write your custom function as a member method in the class?

Thank you, it can be customized. The error you reported here is because you have also defined the getTree function in other places. The error means that you have defined it repeatedly.

There is also a better choice PHP closure, which is probably as follows:

<code>class Test{
    public function index(){

        $hello_closures = function ($str){
            echo $str;
        };

        $hello_closures("hello , php closures function");
    }
}</code>

Look here for the official documentation, it’s easy to understand: http://php.net/manual/zh/func...

The error message is already obvious. You cannot define the getTree function repeatedly. You have already defined it in LabelController

The functions (not class methods) in php are all global, that is to say, whether you define them globally or in functions, they are considered global (this is different from js, Don't get confused).

php does not support function overloading (that is, no duplicate names), so repeated declaration of functions is not allowed (when it comes to overloading, it can be implemented in classes, but the meaning is completely different, it feels like an error handling function, it is just used It’s a friendly display, but I personally think it’s useless).

Statement:
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