You are here: Re: $this is undefined in a class « PHP Programming Language « IT news, forums, messages
Re: $this is undefined in a class

Posted by Peter Jay Salzman on 10/13/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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация