"call to undefined function" error when calling class method

105,585

Solution 1

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

Solution 2

you need to call the function like this

$this->assign()

instead of just assign()

Solution 3

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.

Share:
105,585

Related videos on Youtube

Tommy Arnold
Author by

Tommy Arnold

Updated on February 06, 2020

Comments

  • Tommy Arnold
    Tommy Arnold over 4 years

    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!";}
            };  
        }
    }
    
    • outis
      outis over 13 years
      The sample code might be vulnerable to SQL injection in function delassign. To fix this hole, switch from the outdated mysql driver to PDO and use prepared statements. Note this is superior to explicit sanitization because while you can forget to sanitize, there is nothing security-wise to forget with prepared statement parameters (if you forget a parameter, PHP will generate an error informing you).
    • outis
      outis over 13 years
      ... If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO".
  • mpag
    mpag almost 6 years
    so, a PHP class method whose definition starts with the keyword function is not actually a function? The fact that you need to use $this-> to call it seems inconsistent (IMHO) with not having to pre-define return types (or even if there is an explicit return value).
  • KingCrunch
    KingCrunch almost 6 years
    The keyword "function" in PHP is used functions, methods and procedures (update: "and closures"). It may be a real function, that depends on the context. I don't understand where the relation between $this-> and return types should be. Return types are quite new (at least compared to this answer ;)) and PHP is still under the hood weakly typed. Type declarations are not required.
  • Ace Thanks
    Ace Thanks over 5 years
    "im writing this because couldnt understand why i got this error and this thread is first in google " That's really smart. Thanks.