473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

$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('classDatabase.php');
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">';
StartNavGroup();
NavGroupHeading(LINKS);

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

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

?>
Nov 30 '05 #1
2 1410

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?

--
Oli

Nov 30 '05 #2
Oli Filth <ca***@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
Nov 30 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: agrsaurabh | last post by:
Code Sample:- ATL Class class CAtl : public IA, Public IB
2
by: Quansheng Liang | last post by:
Hello, I struggled with the problem of "undefined reference to `vtable ...`" while migrating a project from windows to linux. After searching the google I removed all the inline functions and now...
3
by: bp | last post by:
Hi, Could someone to tell me what I'm doing wrong? I'm using gcc version 3.3.4 and ld version 2.15.90.0.3 20040415 the linker message is: .../obj/classOpenAreaForm.o(.text+0x211): In...
1
by: Foolster41 | last post by:
I'm rather new to C++ programing. I'm using the dev-C++ program on a windows XP OS. I'm trying to compile the code for a multi user dungeon (MUD) called circle-mud. When I compile I get the...
4
by: r.nikhilk | last post by:
Hi, We are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by including the...
5
by: pavan734 | last post by:
Hi, Iam facing problem with undefined reference linking errors. To explain in more detail.. I have a class called TOP. In its constructor I have instantiated many other class objects using...
2
by: Noah Roberts | last post by:
I have a class, that inherits from a class that inherits virtually from another class. I have a breakdown occuring and it is not wrt the virtually inherited class but one of the other MIed pure...
3
by: s.z.s | last post by:
Hi! I hope the solution to that is not too stupid... I've got three files: <snip test_main.cc> #include"test.hh" int main(void) { A<inta1; a1.saywhat();
10
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
10
by: subramanian100in | last post by:
Consider the following code: #include <iostream> #include <cstdlib> using namespace std; int main() { const double& ref = 100;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.