49

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!";}
        };  
    }
}
duncan
  • 31,401
  • 13
  • 78
  • 99
Tommy Arnold
  • 3,339
  • 8
  • 31
  • 40
  • 4
    The sample code might be vulnerable to [SQL injection](http://unixwiz.net/techtips/sql-injection.html) in function `delassign`. To fix this hole, switch from the outdated mysql driver to [PDO](http://php.net/PDO) and use [prepared statements](http://www.php.net/PDO.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 Jan 26 '11 at 21:33
  • 2
    ... If you need a PDO tutorial, try ["Writing MySQL Scripts with PHP and PDO"](http://www.kitebird.com/articles/php-pdo.html). – outis Jan 26 '11 at 21:34

4 Answers4

108

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()
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 1
    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). – mpag Jun 18 '18 at 19:33
  • 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. – KingCrunch Jun 20 '18 at 07:51
17

you need to call the function like this

$this->assign()

instead of just assign()

Nanne
  • 64,065
  • 16
  • 119
  • 163
4

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.

Vishal
  • 279
  • 3
  • 8
  • 18
0

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.

Edgars Aivars
  • 181
  • 3
  • 11
  • "i`m writing this because couldn`t understand why i got this error and this thread is first in google " That's really smart. Thanks. – Ace Thanks Sep 03 '18 at 05:19