简体   繁体   English

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

[英]“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.

相关问题 PHP,致命错误:调用未定义的方法,从另一个 class 调用 function - PHP, Fatal error: Call to undefined method calling function from another class PHP5.3:从类变量调用调用时出现“调用未定义方法”错误 - PHP5.3: “Call to undefined method” error when calling invoke from class variable 从构造函数调用成员函数时出现“调用未定义函数”错误 - “Call to undefined function” error when calling member function from constructor 调用类方法时,php未定义变量错误 - php undefined variable error when calling class method WordPress-致命错误:调用类时调用成员函数 - WordPress - Fatal error: Call to a member function when calling class 从另一个静态方法调用静态方法:PHP致命错误:调用未定义函数 - Calling a static method from another static method: PHP Fatal error: Call to undefined function 调用类方法时未定义的变量 - Undefined variable when calling a class method PHP:从父级调用静态方法时出现“调用未定义的方法”错误 - PHP: “Call to undefined method” error when calling static method from parent 调用类中的未定义方法,即使已定义 - Call to undefined method in class even when it is defined 调用库函数时发生未定义的属性错误 - Undefined property error when calling a library function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM