Reputation: 3310
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:
I am not able to figure this out. Am I missing something here?
Upvotes: 41
Views: 321204
Reputation: 79
Disclaimer: Am not an expert on PHP.
I had a similar issues while upgrading PHP version for apache2, tried all the suggested solutions but didn't help. Finally found that the php-fpm version enabled (had to do something like -sudo a2enmod php7.4
) and the one present in /etc/apache2 php.ini PHP version were different. Once these were synchronized, the error went away.
Upvotes: 0
Reputation: 11
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.
Upvotes: 0
Reputation: 10945
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.
Upvotes: 1
Reputation: 11
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
Upvotes: 1
Reputation: 91
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();
?>
Upvotes: 8
Reputation: 3310
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:
It was difficult to trace the braces, since the functions were very long - problem with legacy systems. Further steps to troubleshoot were this:
Identified this as some scope issue.
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.
Once the problem identified, cut-pasted the function to the end of file, which solved the issue.
Upvotes: 30
Reputation: 154103
Put this code in a file called p.php
:
<?php
class yoyo{
function salt(){
}
function pepper(){
salt();
}
}
$y = new yoyo();
$y->pepper();
?>
Run it like this:
php p.php
We get error:
PHP Fatal error: Call to undefined function salt() in
/home/el/foo/p.php on line 6
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.
Upvotes: 92
Reputation: 12815
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
Upvotes: 5