40

I am trying to call a function from another function. I get an error:

Fatal error: Call to undefined function getInitialInformation() 
in controller.php on line 24

controller.php file:

require_once("model/model.php"); 

function intake() {
    $info = getInitialInformation($id); //line 24
}

model/model.php

function getInitialInformation($id) {
    return $GLOBALS['em']->find('InitialInformation', $id);
}

Things already tried:

  1. Verified that the require_once works, and the file exists in the specified location.
  2. Verified that the function exists in the file.

I am not able to figure this out. Am I missing something here?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
janenz00
  • 3,315
  • 5
  • 28
  • 37

8 Answers8

92

How to reproduce the error, and how to fix it:

  1. Put this code in a file called p.php:

    <?php
    class yoyo{
        function salt(){
        }
        function pepper(){
            salt();
        }
    }
    $y = new yoyo();
    $y->pepper();
    ?>
    
  2. Run it like this:

    php p.php
    
  3. We get error:

    PHP Fatal error:  Call to undefined function salt() in 
    /home/el/foo/p.php on line 6
    
  4. Solution: use $this->salt(); instead of salt();

    So do it like this instead:

    <?php
    class yoyo{
        function salt(){
        }
        function pepper(){
            $this->salt();
        }
    }
    $y = new yoyo();
    $y->pepper();
    
    ?>
    

If someone could post a link to why $this has to be used before PHP functions within classes, yeah, that would be great.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • I would like to know this too. How to avoid using $this? – SubjectX Dec 12 '14 at 09:54
  • 2
    $this-> refers to a function that belongs to the class and is declared within the class, same thing applies for variables declared inside the class. Inside the class it's called $this->function(), If public outside the class it's called $theclass->function() – multimediaxp Jan 09 '15 at 23:13
  • 1
    Make sure you are not using $this.functionName() or self.functionName() or $self->functionName()..... or variations of. Bit rusty in PHP :-) – Martin Feb 13 '15 at 22:33
  • $this is used because you are attempting to access object properties from within the object itself. This is because when you call 'new myClass()' you create a new instance of that class in memory. – sousdev Aug 30 '16 at 14:13
  • 1
    i was doing the mistake of not using $this->functionName(). What is understand is when you use function from same class, we need to use $this. – Sushank Dahiwadkar Jun 28 '18 at 01:11
  • "class yoyo" is not a scope of methods/variables, and because of that you can not treat salt() as a method included in the scope pepper() is included in. when calling $y->pepper(), $this->salt() is actually calls for $y->salt(), so again pepper() and salt() do not belong to the same scope – Ileana Profeanu Nov 22 '18 at 13:20
  • 1
    Was executing`$this.some_function();` instead of `$this->some_function();`, thanks! – Wouter Vanherck Nov 26 '18 at 13:16
30

This was a developer mistake - a misplaced ending brace, which made the above function a nested function.

I see a lot of questions related to the undefined function error in SO. Let me note down this as an answer, in case someone else have the same issue with function scope.

Things I tried to troubleshoot first:

  1. Searched for the php file with the function definition in it. Verified that the file exists.
  2. Verified that the require (or include) statement for the above file exists in the page. Also, verified the absolute path in the require/include is correct.
  3. Verified that the filename is spelled correctly in the require statement.
  4. Echoed a word in the included file, to see if it has been properly included.
  5. Defined a separate function at the end of file, and called it. It worked too.

It was difficult to trace the braces, since the functions were very long - problem with legacy systems. Further steps to troubleshoot were this:

  1. I already defined a simple print function at the end of included file. I moved it to just above the "undefined function". That made it undefined too.
  2. Identified this as some scope issue.

  3. Used the Netbeans collapse (code fold) feature to check the function just above this one. So, the 1000 lines function above just collapsed along with this one, making this a nested function.

  4. Once the problem identified, cut-pasted the function to the end of file, which solved the issue.

janenz00
  • 3,315
  • 5
  • 28
  • 37
  • I don't see a misplaced end brace... was it not included in the code you provided above? – Michael Mar 22 '15 at 04:13
  • It is not included in the code above, since it was 1000+ lines under. – janenz00 Mar 24 '15 at 14:52
  • 1
    ok, thanks. i had the same identical problem, but it turned out to be a completely different developer mistake, and in the mean time I wanted to make sure I wasn't missing a brace anywhere too, but not seeing the missing brace above was tripping me up. – Michael Mar 24 '15 at 23:04
  • 1
    Troubleshooting the same problem and TIL: in cli you can do `php -l filename.php` to check for syntax errors! – nclsvh Jul 11 '18 at 07:32
8

Many times the problem comes because php does not support short open tags in php.ini file, i.e:

<?
   phpinfo();
?>

You must use:

<?php
   phpinfo();
?>
egig
  • 4,370
  • 5
  • 29
  • 50
JRivero
  • 91
  • 1
  • 3
5

Your function is probably in a different namespace than the one you're calling it from.

http://php.net/manual/en/language.namespaces.basics.php

Jonathan Amend
  • 12,715
  • 3
  • 22
  • 29
1

I happened that problem on a virtual server, when everything worked correctly on other hosting. After several modifications I realized that I include or require_one works on all calls except in a file. The problem of this file was the code < ?php ? > At the beginning and end of the text. It was a script that was only < ?, and in that version of apache that was running did not work

loki
  • 9,816
  • 7
  • 56
  • 82
1

This is obviously not the case in this Q, but since I got here following the same error message I though I would add what was wrong with my code and maybe it will help some one else:

I was porting code from JS to PHP and ended up having a class with some public method. The code that was calling the class (being code that originated from JS) looked something like:

$myObject.method(...)

this is wrong because in PHP it should look like this:

$myObject->method(...)

and it also resulted with "PHP Call to undefined function".

change to use -> and the problem was solved.

epeleg
  • 10,347
  • 17
  • 101
  • 151
0

Presently I am working on web services where my function is defined and it was throwing an error undefined function.I just added this in autoload.php in codeigniter

$autoload['helper'] = array('common','security','url');

common is the name of my controller.

  • I know that as a new user, you cannot comment for short ideas, but if you answer, please make your response a little more robust and describe what your solution actually *does*. See [how to answer](https://stackoverflow.com/help/how-to-answer). As an aside, you can put four spaces in front of any line of text to display it like `this` for readability, or for in-line code, use \`back ticks\` to produce `back ticks`. – alkasm Jun 09 '17 at 10:07
0

Please check that you have <?PHP at the top of your code. If you forget it, this error will appear.