“call to undefined function” error when calling class method

Posted on

“call to undefined function” error when calling class method – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and oop . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about “call to undefined function” error when calling class method.

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!";}

Solution :

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


or 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.


you need to call the function like this


instead of just assign()


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

       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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *