“调用未定义的函数”调用类方法时出错

发布于 2024-10-15 01:08:09 字数 1169 浏览 0 评论 0原文

这是错误致命错误:调用未定义的函数分配(
这是代码,正如你所看到的,我显然已经定义了该函数,所以为什么它不起作用

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赠意 2024-10-22 01:08:09

您没有名为 assign() 的函数,而是具有此名称的方法。 PHP 不是 Java,在 PHP 中你必须明确,如果你想调用函数

assign()

或方法

$object->assign()

,在你的情况下,对函数的调用驻留在另一个方法中。 $this 始终引用其中存在方法的对象本身。

$this->assign()

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()

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->assign()
九厘米的零° 2024-10-22 01:08:09

您需要像这样调用函数,

$this->assign()

而不仅仅是 assign()

you need to call the function like this

$this->assign()

instead of just assign()

萌化 2024-10-22 01:08:09

伙计们,

我今天在测试一个简单的脚本时偶然发现了这个错误。不过,我没有使用“类”函数,所以对它持保留态度。
我在函数定义之前调用函数 &声明...类似这样的东西

      try{
             foo();
         }
       catch (exception $e)
           {
            echo "$e->getMessage()";
           }

       function foo(){
                       echo "blah blah blah";
                     }

,所以 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

      try{
             foo();
         }
       catch (exception $e)
           {
            echo "$e->getMessage()";
           }

       function foo(){
                       echo "blah blah blah";
                     }

so php was throwing me error "call to undefined function ".

This kinda seem classic programming error but may help someone in need of clue.

洋洋洒洒 2024-10-22 01:08:09

您可以犯的另一个愚蠢的错误是将递归函数从非类环境复制到类,并且不要将内部自我调用更改为 $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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文