php 调用类方法时出现“调用未定义函数”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4809702/
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
"call to undefined function" error when calling class method
提问by Tommy Arnold
this is the error Fatal error: Call to undefined function assign(
this is the code, as you can see i obviously have defined the function so why is it not working
这是错误,Fatal error: Call to undefined function assign(
这是代码,正如您所看到的,我显然已经定义了函数,为什么它不起作用
class shades {
function create($name, $shades, $slug, $shortDesc, $longDesc, $position){
$name = sanitize_paranoid_string($name);
$slug = slug($name);
$shortDesc = sanitize_sql_string($shortDesc);
$longDesc = sanitize_sql_string($longDesc);
$query = mysql_query("INSERT INTO products (type, name, slug, shortDesc, htmlDesc, position)VALUES('shades','$name','$slug','$shortDesc','$longDesc','$position')")or die(mysql_error());
$ID = mysql_insert_id();
assign($shades, $ID);
if($query) {return true;}
else {return false;};
}
function delassign($toID){
mysql_query("DELETE FROM assign WHERE type='shades' AND toID='$toID'")or die(mysql_error());
}
function assign($shades, $toID)
{
foreach($shades as $shade)
{
$result = mysql_query("INSERT INTO assign(type, typeID, toID)VALUES('shades','$shade','$toID')")or die(mysql_error());
if($result){echo "Added!";}
else{echo"Not Added!";}
};
}
}
回答by KingCrunch
You dont have a function named assign()
, but a method with this name. PHP is not Java and in PHP you have to make clear, if you want to call a function
您没有名为 的函数assign()
,而是具有此名称的方法。PHP 不是 Java,在 PHP 中你必须明确,如果你想调用一个函数
assign()
or a method
或方法
$object->assign()
In your case the call to the function resides inside another method. $this
always refers to the object, in which a method exists, itself.
在您的情况下,对该函数的调用驻留在另一个方法中。$this
总是指方法所在的对象本身。
$this->assign()
回答by Nanne
you need to call the function like this
你需要像这样调用函数
$this->assign()
instead of just assign()
而不仅仅是 assign()
回答by Vishal
Mates,
伙伴们,
I stumbled upon this error today while testing a simple script. I am not using "class" function though so it take it with grain of salt. I was calling function before its definition & declaration ...something like this
我今天在测试一个简单的脚本时偶然发现了这个错误。我没有使用“类”函数,所以它带着一粒盐。我在定义和声明之前调用函数......像这样
try{
foo();
}
catch (exception $e)
{
echo "$e->getMessage()";
}
function foo(){
echo "blah blah blah";
}
so php was throwing me error "call to undefined function ".
所以php向我抛出错误“调用未定义的函数”。
This kinda seem classic programming error but may help someone in need of clue.
这有点像经典的编程错误,但可能会帮助需要线索的人。
回答by Edgars Aivars
Another silly mistake you can do is copy recursive function from non class environment to class and don`t change inner self calls to $this->method_name()
您可以做的另一个愚蠢的错误是将递归函数从非类环境复制到类,并且不要将内部自我调用更改为 $this->method_name()
i`m writing this because couldn`t understand why i got this error and this thread is first in google when you search for this error.
我写这个是因为不明白为什么我会收到这个错误,当你搜索这个错误时,这个线程在谷歌中是第一个。