$this is undefined in a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Jay Salzman

    $this is undefined in a class

    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?





    <?
    require_once('c lassDatabase.ph p');


    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("SELEC T * FROM $tblLnkGrp");
    while ( $row = $db->fetchArray() )
    $this->groups[$row['id']] = $row;

    // Store links in array indexed by link id.
    //
    $db->query("SELEC T * 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="sele cted"' : '';
    $output .= "<option value=\"$grpId\ " $selected>$grpN ame</option>\n";
    }

    echo $output . '</select>';
    }





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

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

    EndNavGroup();
    echo '</div> <!-- nav-links -->';
    }


    }

    ?>
  • Oli Filth

    #2
    Re: $this is undefined in a class


    Peter Jay Salzman wrote:[color=blue]
    > 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?
    >[/color]
    <...SNIP CODE...>

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

    --
    Oli

    Comment

    • Peter Jay Salzman

      #3
      Re: $this is undefined in a class

      Oli Filth <catch@olifilth .co.uk> wrote:[color=blue]
      >
      > Peter Jay Salzman wrote:[color=green]
      >> 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?
      >>[/color]
      > <...SNIP CODE...>
      >
      > Can you post some (short) example code of how you are using the class
      > and calling its methods?[/color]

      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']) )
      touchLinksSessi on();



      function touchLinksSessi on()
      {
      $_SESSION['links'] = new LinksClass;
      }



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


      <td>
      <input type="text" name="frmLnkTit le" class="text"
      value="<?= @ $theLnkTitle ?>" />
      </td>
      <td>Group:</td>
      <td><?php LinksClass::sel ectGroup($row['grpid']) ?></td>
      <input type="submit" name="action" class="sub" value="editlnk" />
      <input type="submit" name="action" class="sub" value="deleteln k"
      onclick="return deleteConfirm() ;" />
      </td>



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

      LinksClass::sel ectGroup($row['grpid'])

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

      $_SESSION['links']->selectGroup($r ow['grpid'])

      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.

      Thanks!
      Pete

      Comment

      Working...