In PHP, how do I check if a function exists?

后端 未结 6 1111
挽巷
挽巷 2020-11-30 11:00

How can I check if the function my_function already exists in PHP?

6条回答
  •  广开言路
    2020-11-30 11:27

    And if my_function is in namespace:

    namespace MyProject;
    
    function my_function() {
        return 123;
    }
    

    You can check if it exists

    function_exists( __NAMESPACE__ . '\my_function' );
    

    in the same namespace or

    function_exists( '\MyProject\my_function' );
    

    outside of the namespace.

    P.S. I know this is a very old question and PHP documentation improved a lot since then, but I believe people still taking a peek here, and this might be helpful.

提交回复
热议问题