We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Fatal error: Using $this when not in object context

Development and Programming

lebisol's avatar
lebisol
2,234 posts
14 years ago
lebisol's avatar lebisol

Not sure what is going on here….not exactly a developer just trying to learn. For now the idea is that code snip can be pasted into template but eventually would want to learn how to make a simple extension out of it.

Thank you.

EDIT NOTE: it seems that if I remove the use of functions it gets processed right…what gives? Can we not put queries inside functions?

Fatal error: Using $this when not in object context in /sandbox/ee2/system/expressionengine/libraries/Functions.php(656) : eval()’d code on line 15
<?php
// =============================================================
//  Delete all comments that are made by guests - nonmembers
//  and update comment counter
// =============================================================


// --- 'Show Comments'
function find_comments()
{
// connect to database
 global $db;

// SHOW comments that will be deleted
$show_comments = $this->EE->db->query ("SELECT * FROM exp_comments WHERE NOT EXISTS (SELECT * FROM exp_members WHERE exp_members.member_id = exp_comments.author_id)");

 if ($show_comments->num_rows > 0)
 {
     echo "Name   »   Comment 
<hr >\n";
  foreach($show_comments->result as $row)
  {
   echo $row['name'].'  »  '.$row['comment']."
<hr >\n";
  }
 }
else {
        echo "All of the comments are made by members.";
    }
}
// --- END 'Show Comments'
// execute
find_comments();

// --- 'Delete Comments'
function delete_comments ()
{
// connect to database
 global $db;
//  DELETE the comments shown above
 $delete_comments = $this->EE->db->query("DELETE FROM exp_comments WHERE NOT EXISTS (SELECT * FROM exp_members WHERE exp_members.member_id = exp_comments.author_id)");
  echo $this->EE->db->affected_rows." comments were deleted.";

}
// execute
delete_comments();
// --- END 'Delete Comments'

// build form here

?>
       
the3mus1can's avatar
the3mus1can
426 posts
14 years ago
the3mus1can's avatar the3mus1can

$this is a special variable that can only be used inside of a method/function that is bound to an instance of a class.

Ex.

class Person {
    function get_name()
    {
        return $this->name;
    }
}

For your code you do not need to use $this. Try something like this (pardon the pun):

<?php
// =============================================================
//  Delete all comments that are made by guests - nonmembers
//  and update comment counter
// =============================================================


// --- 'Show Comments'
function find_comments()
{
// connect to database
$EE = get_instance();

// SHOW comments that will be deleted
$show_comments = $EE->db->query ("SELECT * FROM exp_comments WHERE NOT EXISTS (SELECT * FROM exp_members WHERE exp_members.member_id = exp_comments.author_id)");

 if ($show_comments->num_rows > 0)
 {
     echo "Name   »   Comment 
<hr >\n";
  foreach($show_comments->result as $row)
  {
   echo $row['name'].'  »  '.$row['comment']."
<hr >\n";
  }
 }
else {
        echo "All of the comments are made by members.";
    }
}
// --- END 'Show Comments'
// execute
find_comments();

// --- 'Delete Comments'
function delete_comments ()
{
// connect to database
$EE = get_instance();
//  DELETE the comments shown above
 $delete_comments = $EE->db->query("DELETE FROM exp_comments WHERE NOT EXISTS (SELECT * FROM exp_members WHERE exp_members.member_id = exp_comments.author_id)");
  echo $EE->db->affected_rows." comments were deleted.";

}
// execute
delete_comments();
// --- END 'Delete Comments'

// build form here

?>
       
Ian from Aus's avatar
Ian from Aus
94 posts
14 years ago
Ian from Aus's avatar Ian from Aus

(edit: damn… not quite quick enough)

afaik, because the code in a template is eval()’d, it isn’t an object and therefore doesn’t have a $this variable. In addition to that, the EE object isn’t referenced and so not available either.

You could try to get access to the EE object with the same function call as a module would use, just without reference to the $this object:

$EE =& get_instance();

(the variable “$EE” is flexible of course)

You would then reference this for a query with:

$EE->db->query
       
lebisol's avatar
lebisol
2,234 posts
14 years ago
lebisol's avatar lebisol

Thank you both. Now it makes more sense…I did not realize that $EE was a flexible variable. Is this EE 2 specific way of connecting to db?

$EE = get_instance();

Docs don’t have this referenced. Thank you.

       
the3mus1can's avatar
the3mus1can
426 posts
14 years ago
the3mus1can's avatar the3mus1can

That is because you where the context of a function. If you were not in a function it would have been fine.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.