Fatal error: using $ this if not in object context

Does anyone know why this code is giving an error? "Fatal error: Using $ this if not in the context of an object" This is the part of the script I am trying to get.

thank

    function add($is_general_media, $title, $description, $allowed_extensions, $display) {
    $db = new db;

    if (User::isAdmin()) { 
        $parentID = $db->sanitize_to_db($parentID);
        $is_general_media = $db->sanitize_to_db($is_general_media);
        $title = $db->sanitize_to_db($title);
        $description = $db->sanitize_to_db($description);
        $allowed_extensions = $db->sanitize_to_db($allowed_extensions);
        $display = ($display == 'grid') ? 'grid' : 'list';
        if (strtolower(get_class($this)) == "mediaarea"); {
             function parentID();{
            $parentID = $this->getID();
            }
        } else {
            $parentID = 0;
        }
        if (!$title) {
            $title = '(Untitled Area)';
        }
        $q = "insert into DarkRoom_Areas (title, description, image_max_width, image_max_height, image_max_thumbnail_width, image_max_thumbnail_height, is_general_media, parent_id, allowed_extensions, display) values ('$title','$description'," . MEDIA_DEFAULT_MAX_WIDTH . "," . MEDIA_DEFAULT_MAX_HEIGHT . "," . MEDIA_DEFAULT_MAX_THUMBNAIL_WIDTH . "," . MEDIA_DEFAULT_MAX_THUMBNAIL_HEIGHT . ",  $is_general_media, $parentID, '$allowed_extensions', '$display')";
        $r = mysql_query($q);
        if ($r) {
            $ma = MediaArea::get(mysql_insert_id());
            return $ma;
        } else {
            $e = new Error();
            $e->add(mysql_error());
            return $e;
        }
    }
}

      

0


source to share


1 answer


If you don't provide a class declaration, it looks like a normal function. You can $this

only use it inside a class. Try checking PHP docs on classes for more information.

In addition, this feature:



function parentID();{
    $parentID = $this->getID();
}

      

will cause problems regardless. Since $this

it is not in scope parentID()

, you need to pass it as a parameter and change the variable name.

+2


source







All Articles