How to Call to Undefined Function in PHP

Shraddha Paghdar Feb 02, 2024
PHP
How to Call to Undefined Function in PHP

Many of you have encountered this error several times Fatal error: Call to undefined function function_name(). In today’s post, we are finding out how to unravel this error. But before we solve this problem, let’s understand how PHP evaluates the functions.

There are several ways to define functions and call them. Let’s say you write it in the function.php file and call it in the main.php file.

    // function.php
<?php
    namespace fooNamespace {
        function foo() {
            return "Calling foo"
        }
    }
?>
    // main.php
include function.php
<?php
    echo fooNamespace\foo();
?>

Namespaces are qualifiers that enable better management by grouping classes that work together to perform a task. It allows you to use the same name for multiple classes. It is important to know how PHP knows which element of the namespace is required by the code. PHP namespaces work quite a sort of a filesystem. There are 3 ways to access a file in a file system:

  1. Relative file name such as fooBar.txt. It will resolve to fooDirectory/fooBar.txt where fooDirectory is the directory currently busy directory.
  2. Relative path name such as subdirectory/fooBar.txt. It will resolve to fooDirectory/subdirectory/fooBar.txt.
  3. Absolute path name such as /main/fooBar.txt. It will resolve to /main/fooBar.txt.

Namespaced elements in PHP follow an equivalent principle. For example, a class name can be specified in three ways:

  1. Unqualified name/Unprefixed class name:
    $a = new foo();
    
    Or,
    foo::staticmethod();
    
    If the current namespace is foonamespace, it will always resolve to foonamespace\foo. If the code is a global, non-namespaced code, this resolves to foo.
  2. Qualified name/Prefixed class name:
    $a = new fooSubnamespace\foo();
    
    Or,
    fooSubnamespace\foo::staticmethod();
    
    If the present namespace is foonamespace, it will always resolve to foonamespace\fooSubnamespace\foo. If the code is global, non-namespaced code, this resolves to fooSubnamespace\foo.
  3. Fully qualified name/Prefixed name with global prefix operator:
    $a = new \foonamespace\foo();
    
    Or,
    \foonamespace\foo::staticmethod();
    
    This always resolves to the literal name laid out in the code, foonamespace\foo.

Now suppose you define a class & call the method of a class within the same namespace.

<?php
    class foo {
        function barFn() {
          echo "Hello foo!"
        }
        function bar() {
            barFn();
            // interpreter is confused which instance's function is called
            $this->barFn();
        }
    }
    $a = new foo();
    $a->bar();
?>

$this pseudo-variable has the methods and properties of the current object. Such a thing is beneficial because it allows you to access all the member variables and methods of the class. Inside the class, it is called $this->functionName(). Outside of the class, it is called $theclass->functionName().

$this is a reference to a PHP object the interpreter created for you, which contains an array of variables. If you call $this inside a normal method in a normal class, $this returns the object to which this method belongs.

Steps to Resolve the Error of Calling to Undefined Function in PHP

  • Verify that the file exists. Find the PHP file in which the function definition is written.
  • Verify that the file has been included using the require (or include) statement for the above file on the page. Check that the path in the require/include is correct.
  • Verify that the file name is spelled correctly in the require statement.
  • Print/echo a word in the included file to ascertain if it has been properly included.
  • Defined a separate function at the end of the file and call it.
  • Check functions are closed properly. (Trace the braces)
  • If you are calling methods of a class, make sure $this-> is written.
Shraddha Paghdar avatar Shraddha Paghdar avatar

Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.

LinkedIn