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

This question is related to php oop class methods

The answer is


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.


you need to call the function like this

$this->assign()

instead of just assign()


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.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to oop

How to implement a simple scenario the OO way When to use 'raise NotImplementedError'? PHP: cannot declare class because the name is already in use Python class input argument Call an overridden method from super class in typescript Typescript: How to extend two classes? What's the difference between abstraction and encapsulation? An object reference is required to access a non-static member Java Multiple Inheritance Why not inherit from List<T>?

Examples related to class

String method cannot be found in a main class method Class constructor type in typescript? ReactJS - Call One Component Method From Another Component How do I declare a model class in my Angular 2 component using TypeScript? When to use Interface and Model in TypeScript / Angular Swift Error: Editor placeholder in source file Declaring static constants in ES6 classes? Creating a static class with no instances In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric Static vs class functions/variables in Swift classes?

Examples related to methods

String method cannot be found in a main class method Calling another method java GUI ReactJS - Call One Component Method From Another Component multiple conditions for JavaScript .includes() method java, get set methods includes() not working in all browsers Python safe method to get value of nested dictionary Calling one method from another within same class in Python TypeError: method() takes 1 positional argument but 2 were given Android ListView with onClick items