1

This applies to PHP CLI (command line interface) on Windows and linux. This is not a problem when using PHP via HTTP.

I am getting a PHP Fatal error:  Call to undefined function when I put a function in a separate file.

This is regardless of using include, require, or require_once.

The code works great if I have the functions in the same file, but that's not practical.

Example

runMe.php

<?php
    var_dump($argv);           //<----command line - array of arguments

    if(file_exists('functions.php')) require_once('functions.php'); //<--- load functions

    testme();                 //<---- call function    
?>

functions.php    The testme() function does nothing.

<?php    
    function testme(){
        //do nothing
    }
?>

The following suggestion does not work: php -r "include('functions.php'); testme();" Found at: How to call a PHP function from CLI?

Another suggestion (creating a class) does not work either: How do you execute a method in a class from the command line

All other suggestions that I found used include and require.

Up2Long
  • 11
  • 1
  • You do realise that it will be looking for functions.php in the folder where you're running the script, and folders in the include path, not necessarily where the script itself is contained – Mark Baker Oct 31 '17 at 17:52
  • Make sure error reporting and display is on and remove the `if(file_exists('functions.php'))` – AbraCadaver Oct 31 '17 at 17:54
  • use chdir to set the current directory to where your script resides, then you can use relative path to access your functions.php – Jeff Beagley Oct 31 '17 at 17:55
  • Mark, the functions.php is in the same folder. – Up2Long Oct 31 '17 at 18:08
  • AbraCadaver, that does not matter. The if statement just proves that the file exists and loads it. A simple php ruMe.php will display the contents of the included file. – Up2Long Oct 31 '17 at 18:08
  • Jeff, the files are all in the same directory. And, it would not load if the if (statement) did not see it. A simple php ruMe.php will display the contents of the included file. – Up2Long Oct 31 '17 at 18:08
  • I just tried it and in my first script (that would be runMe.php for you), I added this in the first line: #!//usr/bin/php This is on linux. The path would be different on your setup. And it works ok for me...? – Nic3500 Oct 31 '17 at 18:25
  • If I run get_include_path(), I have this: .:/php/includes:/home/nic3500/tools/php/lib/php. Notice how it starts with the '.' ? You might not have that in your include path... – Nic3500 Oct 31 '17 at 18:27
  • Nic3500, neither suggestion resolves the problem. PHP is working fine. Prior to posting I played with other path statements to rule this out. Again, the path is fine and can be witnessed when running the program from the command line: php runMe.php you will see the functions.php is echoed to the console (on both Windows & Linux). To suppress that use: php -r runMe.php – Up2Long Nov 01 '17 at 01:28
  • Today, almost a month later, the functions in other files work. I made no changes to the code or the PC. The only things that took place were updates by Microsoft and Mozilla. I have no clue what was fixed/changed in the background. **!@#$@#@$(&)%^$%%^** – Up2Long Nov 26 '17 at 18:43

0 Answers0