[英]“call to undefined function” error when calling class method
this is the error Fatal error: Call to undefined function assign(
这是错误
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 这是代码,你可以看到我显然定义了函数,为什么它不起作用
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!";}
};
}
}
You dont have a function named assign()
, but a method with this name. 你没有名为
assign()
的函数,而是一个具有此名称的方法。 PHP is not Java and in PHP you have to make clear, if you want to call a function 如果你想调用一个函数,那么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()
you need to call the function like this 你需要像这样调用这个函数
$this->assign()
instead of just assign()
而不仅仅是
assign()
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. 这有点似乎是经典的编程错误,但可能会帮助需要线索的人。
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. 我写这个是因为无法理解为什么我收到这个错误,当你搜索这个错误时,这个帖子在google中是第一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.