Page 1 of 1
Member variables PHP (5) classes
Posted: Sat Feb 03, 2007 1:57 pm
by mjseaden
Hi,
I'm defining a 'SiteGenerate' class. I define it like this:
Code: Select all
class SiteGenerate
{
function __construct()
{
}
function __destruct()
{
}
public function Initialise()
{
// Connect to database
$this->$m_database = new Database;
if(!$this->$m_database->Initialise())
{
$m_returncode = $m_database->GetReturnCode();
return false;
}
return true;
}
.....
private $m_database;
}
For some reason, although $m_database is defined as a member variable in the class (I do this so that I can access the instance of the database class outside of the scope of Initialise()) I keep getting the error
Code: Select all
Fatal error: Cannot access empty property in C:\Apache\Apache2\htdocs\sitegen.php on line 129
where line 129 is the "$this->$m_database = new Database();" line. Why is this happening? I've tried moving $m_database above the constructors and destructors, but it doesn't seem to make any difference.
Posted: Sat Feb 03, 2007 2:22 pm
by alex.barylski
Edit: LOL I just re-read your title and that was 4 PHP and not PHP 4

my bad...
[s]Well for starters...PHP4 doesn't support the private,protected and public member variable access modifiers...
Are you using PHP 4 or 5?[/s]
$this->$m_database
Should be:
$this->m_database
Drop the '$' from m_database???
Posted: Sat Feb 03, 2007 2:47 pm
by mjseaden
Hi Hockey,
Thanks for the suggestion - that looked as if it worked, it doesn't seem to give the same errors but now I'm getting the error
Code: Select all
Parse error: parse error, unexpected T_STRING, expecting T_VARIABLE in C:\Apache\Apache2\htdocs\sitegen.php on line 387
Posted: Sat Feb 03, 2007 2:56 pm
by Ollie Saunders
OK just so there is no confusion please remove the '4' from the subject of your initial post and tell us what your actual PHP version is.
Code: Select all
Parse error: parse error, unexpected T_STRING, expecting T_VARIABLE in C:\Apache\Apache2\htdocs\sitegen.php on line 387
What file is that? What line is that?
Posted: Sat Feb 03, 2007 2:59 pm
by mjseaden
Sorry, the line was
in the member declarations for the class.
I've put the '$' back, as a '$' is used in the PHP manual.
I've also tried giving $m_database a default value of zero:
Which has not improved matters.
I'm struggling to see why the definition of a member is causing such problems.
Posted: Sat Feb 03, 2007 3:03 pm
by Ollie Saunders
Code: Select all
return true;
}
.....
private $m_database;
}
The problem is almost certainly a result of whatever is happening in those dots.... please post the actual code.
Posted: Sat Feb 03, 2007 3:09 pm
by mjseaden
The compiler seems to be getting through the rest of it okay, it seems to centralise around the definition of the member variable.
Could it be something to so with the fact that if I declare a member variable, it becomes a T_VARIABLE, but that it cannot be converted to an object when I try and set it equal to a new Class(); ?
For example. if I do
straight off, without first defining $database, is $database ever a T_VARIABLE?
Posted: Sat Feb 03, 2007 3:16 pm
by nickvd
Perhaps if you post the actual code that has a problem we can help you...
$this->database = new Database(); will work just fine.
We still need to know what version of php you use... I haven't seen you post that information.
Posted: Sat Feb 03, 2007 3:18 pm
by mjseaden
Got it.
I'm used to the 'this' operator in C++, but it works differently in PHP.
I should define it as
but then
access it in the class as
because the '$' on 'this' indicates the variable, I don't also need to indicate $m_database is a variable:
as I was doing in the code. Cheers for those who have helped.
Posted: Sat Feb 03, 2007 3:30 pm
by nickvd
Bingo!

Glad you got it sorted...
Posted: Sat Feb 03, 2007 3:33 pm
by Ollie Saunders
$object->$property uses the value of $property as the property name so.
Code: Select all
$property = 'foo';
$obj = new stdClass();
$obj->foo = 12;
echo $obj->$property; // outputs 12