|
Posted by Peter Jay Salzman on 10/29/87 11:33
Oli Filth <catch@olifilth.co.uk> wrote:
>
> Peter Jay Salzman wrote:
>> 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?
>>
> <...SNIP CODE...>
>
> Can you post some (short) example code of how you are using the class
> and calling its methods?
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']) )
touchLinksSession();
function touchLinksSession()
{
$_SESSION['links'] = new LinksClass;
}
I have a page that lets me edit links. Here's code for that:
<td>
<input type="text" name="frmLnkTitle" class="text"
value="<?= @ $theLnkTitle ?>" />
</td>
<td>Group:</td>
<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();" />
</td>
Oh, actually, I think I see the problem. Instead of using
LinksClass::selectGroup($row['grpid'])
which doesn't construct the class, I should use:
$_SESSION['links']->selectGroup($row['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
[Back to original message]
|