“调用未定义的函数”调用类方法时出错
这是错误致命错误:调用未定义的函数分配(
这是代码,正如你所看到的,我显然已经定义了该函数,所以为什么它不起作用
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!";}
};
}
}
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
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!";}
};
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有名为
assign()
的函数,而是具有此名称的方法。 PHP 不是 Java,在 PHP 中你必须明确,如果你想调用函数或方法
,在你的情况下,对函数的调用驻留在另一个方法中。
$this
始终引用其中存在方法的对象本身。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 functionor a method
In your case the call to the function resides inside another method.
$this
always refers to the object, in which a method exists, itself.您需要像这样调用函数,
而不仅仅是
assign()
you need to call the function like this
instead of just
assign()
伙计们,
我今天在测试一个简单的脚本时偶然发现了这个错误。不过,我没有使用“类”函数,所以对它持保留态度。
我在函数定义之前调用函数 &声明...类似这样的东西
,所以 php 向我抛出错误“调用未定义的函数”。
这看起来有点经典的编程错误,但可能会帮助需要线索的人。
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
so php was throwing me error "call to undefined function ".
This kinda seem classic programming error but may help someone in need of clue.
您可以犯的另一个愚蠢的错误是将递归函数从非类环境复制到类,并且不要将内部自我调用更改为 $this->method_name()
我写这篇文章是因为无法理解为什么我会收到此错误和此错误当您搜索此错误时,线程在谷歌中排名第一。
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()
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.