Home > Article > Backend Development > Recursion - Can I write custom functions in PHP member methods?
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
The error was reported as follows
But I have another method that also writes functions and performs recursion, but no error is reported.
This is the interface for calling these two methods
Why is an error reported? Can anyone explain it or have a good way to deal with it
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
The error was reported as follows
But I have another method that also writes functions and performs recursion, but no error is reported.
This is the interface for calling these two 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).