PHP 调用未定义的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14115886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Call to undefined function
提问by janenz00
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:
控制器.php文件:
require_once("model/model.php");
function intake() {
$info = getInitialInformation($id); //line 24
}
model/model.php
模型/model.php
function getInitialInformation($id) {
return $GLOBALS['em']->find('InitialInformation', $id);
}
Things already tried:
已经尝试过的事情:
- Verified that the require_once works, and the file exists in the specified location.
- Verified that the function exists in the file.
- 验证 require_once 是否有效,并且文件存在于指定位置。
- 验证该函数是否存在于文件中。
I am not able to figure this out. Am I missing something here?
我无法弄清楚这一点。我在这里错过了什么吗?
采纳答案by janenz00
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.
我看到很多与 SO 中未定义函数错误相关的问题。让我记下这个作为答案,以防其他人对function scope有同样的问题。
Things I tried to troubleshoot first:
我首先尝试解决的问题:
- Searched for the php file with the function definition in it. Verified that the file exists.
- Verified that the require(or include) statement for the above file exists in the page. Also, verified the absolute path in the require/includeis correct.
- Verified that the filename is spelled correctly in the require statement.
- Echoed a word in the included file, to see if it has been properly included.
- Defined a separate function at the end of file, and called it. It worked too.
- 搜索包含函数定义的 php 文件。验证文件存在。
- 验证页面中是否存在上述文件的require(或include)语句。另外,验证了require/include 中的绝对路径是否正确。
- 验证文件名在 require 语句中拼写正确。
- 在包含的文件中回显一个单词,看看它是否被正确包含。
- 在文件末尾定义一个单独的函数,并调用它。它也起作用了。
It was difficult to trace the braces, since the functions were very long - problem with legacy systems. Further steps to troubleshoot were this:
很难追踪大括号,因为函数很长 - 遗留系统的问题。排除故障的进一步步骤是:
- 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.
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.
- 我已经在包含文件的末尾定义了一个简单的打印函数。我将它移到“未定义函数”的正上方。这也使它未定义。
将此确定为某个范围问题。
使用 Netbeans 折叠(代码折叠)功能来检查该功能正上方的功能。因此,上面的 1000 行函数与此函数一起折叠,使其成为嵌套函数。
发现问题后,将该函数剪切粘贴到文件末尾,从而解决了问题。
回答by Eric Leschinski
How to reproduce the error, and how to fix it:
如何重现错误,以及如何修复它:
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.phpWe get error:
PHP Fatal error: Call to undefined function salt() in /home/el/foo/p.php on line 6Solution: use
$this->salt();instead ofsalt();So do it like this instead:
<?php class yoyo{ function salt(){ } function pepper(){ $this->salt(); } } $y = new yoyo(); $y->pepper(); ?>
将此代码放在名为 的文件中
p.php:<?php class yoyo{ function salt(){ } function pepper(){ salt(); } } $y = new yoyo(); $y->pepper(); ?>像这样运行它:
php p.php我们得到错误:
PHP Fatal error: Call to undefined function salt() in /home/el/foo/p.php on line 6解决方案:使用
$this->salt();代替salt();所以这样做:
<?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.
如果有人可以发布一个链接,说明为什么必须在类中的 PHP 函数之前使用 $this,那就太好了。
回答by JRivero
Many times the problem comes because phpdoes not support short open tags in php.inifile, i.e:
很多时候问题是因为php不支持php.ini文件中的短打开标签,即:
<?
phpinfo();
?>
You must use:
您必须使用:
<?php
phpinfo();
?>
回答by Jonathan Amend
Your function is probably in a different namespace than the one you're calling it from.
您的函数可能位于与您从中调用它的命名空间不同的命名空间中。
回答by Asier Arizti
I happened that problem on a virtual server, when everything worked correctly on other hosting.
After several modifications I realized that I includeor require_oneworks 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
我在虚拟服务器上发生了这个问题,但在其他主机上一切正常。若干修改后,我意识到,我include还是require_one作品上,除了文件中的所有电话。这个文件的问题是代码< ?php ? >在文本的开头和结尾。它只是一个脚本< ?,并且在运行的那个版本的 apache 中不起作用
回答by user8136352
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
目前,我正在研究定义了我的函数的 Web 服务,它抛出了一个错误未定义的函数。我只是在 codeigniter 的 autoload.php 中添加了这个
$autoload['helper'] = array('common','security','url');
$autoload['helper'] = array('common','security','url');
common is the name of my controller.
common 是我的控制器的名称。

