$this is undefined in a class

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a very simple class: 2 variables, 3 methods.  PHP is telling me that
$this is undefined in one of the functions, but is defined and has the
expected value in the other two functions.

In the code before, $this is defined in LinksClass() and navbarLinks(), but
not selectGroup().  I've never seen anything like this before.   What could
possibly be causing this?


Class LinksClass
   var $groups;
   var $links;

function LinksClass()
   $db = new DatabaseClass;
   $tblLnkGrp = 'pbtbl_lnkgrp';
   $tblLinks = 'pbtbl_links';

   // Store linkgroups in array indexed by linkgroup id.
   $db->query("SELECT * FROM $tblLnkGrp");
   while ( $row = $db->fetchArray() )
      $this->groups[$row['id']] = $row;

   // Store links in array indexed by link id.
   $db->query("SELECT * FROM $tblLinks");
   while ( $row = $db->fetchArray() )
      $this->links[$row['id']] = $row;


// Creates a select menu of the linkgroups.   UNUSED
function selectGroup( $activeGroupId = '' )
echo '<pre>'; print_r($this); echo '</pre>'; exit;
   $output = '<select name="lnkGrp">';

   foreach ( $this->groups as $group )
      $grpId   = $group['id'];
      $grpName = $group['name'];
      $selected = ( $activeGroupId == $grpId ) ? 'selected="selected"' : '';
      $output .= "<option value=\"$grpId\" $selected>$grpName</option>\n";

   echo $output . '</select>';

function navbarLinks()
   echo '<div id="nav-links">';

   foreach ( $this->links as $lnk )
      NavItem($lnk['url'], $lnk['text'], $lnk['title']);

   echo '</div> <!-- nav-links -->';



Re: $this is undefined in a class

Peter Jay Salzman wrote:
Quoted text here. Click to load it
<...SNIP CODE...>

Can you post some (short) example code of how you are using the class
and calling its methods?


Re: $this is undefined in a class

Quoted text here. Click to load it
It's a bit complicted (probably because I'm still inexperienced with PHP).

I'm writing a blog and store various things in session variables to cache
them so the database doesn't have to be constantly queried.  Each page
includes code like:

   if ( ! isset($_SESSION['links']) )

   function touchLinksSession()
         $_SESSION['links'] = new LinksClass;

I have a page that lets me edit links.  Here's code for that:

<input type="text" name="frmLnkTitle" class="text"
<td><?php LinksClass::selectGroup($row['grpid']) ?></td>
   <input type="submit" name="action" class="sub" value="editlnk" />
   <input type="submit" name="action" class="sub" value="deletelnk"
      onclick="return deleteConfirm();" />

Oh, actually, I think I see the problem.  Instead of using


which doesn't construct the class, I should use:


which DOES construct the class.  That's why $this wasn't defined.  Holy cow.
I can't believe I spent over an hour hunting for this.   :(

I'm glad you told me to post example code; it focused my eye on what I
should've been looking at: where the code used, rather than where the class
is defined.  Valuable lesson learned.


Site Timeline