php PHP致命错误:定义函数时调用未定义的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10817133/
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 Fatal error: Call to undefined function when function is defined
提问by Ben Alter
I am getting an error in PHP:
我在 PHP 中遇到错误:
PHP Fatal error: Call to undefined function getCookie
Code:
代码:
include('Core/dAmnPHP.php');
$tokenarray = getCookie($username, $password);
Inside of dAmnPHP.php, it includes a function called getCookie inside class dAmnPHP. When I run my script it tells me that the function is undefined.
在 dAmnPHP.php 中,它在类 dAmnPHP 中包含一个名为 getCookie 的函数。当我运行脚本时,它告诉我该函数未定义。
What am I doing wrong?
我究竟做错了什么?
回答by Maythe
It looks like you need to create a new instance of the class before you can use its functions.
看起来您需要先创建该类的新实例,然后才能使用其功能。
Try:
$dAmn = new dAmnPHP;
$dAmn->getCookie($username, $password);
尝试:
$dAmn = new dAmnPHP;
$dAmn->getCookie($username, $password);
I've not used dAmn before, so I can't be sure, but I pulled my info from here: https://github.com/DeathShadow/Contra/blob/master/core/dAmnPHP.php
我以前没有使用过 dAmn,所以我不能确定,但我从这里提取了我的信息:https: //github.com/DeathShadow/Contra/blob/master/core/dAmnPHP.php
回答by Eric Leschinski
How to reproduce this error:
如何重现此错误:
Put this in a file called a.php:
把它放在一个名为 a.php 的文件中:
<?php
include('b.php');
umad();
?>
Put this in a file called b.php:
把它放在一个名为 b.php 的文件中:
<?php
class myclass{
function umad(){
print "ok";
}
}
?>
Run it:
运行:
PHP Fatal error: Call to undefined function umad() in
/home/el/a.php on line 4
What went wrong:
什么地方出了错:
You can't use methods inside classes without instantiating them first. Do it like this:
你不能在类中使用方法而不先实例化它们。像这样做:
<?php
include('b.php');
$mad = new myclass;
$mad->umad();
?>
Then the php interpreter can find the method:
然后php解释器就可以找到方法了:
eric@dev ~ $ php a.php
ok

