Member variables PHP (5) classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Member variables PHP (5) classes

Post 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.
Last edited by mjseaden on Sat Feb 03, 2007 2:58 pm, edited 1 time in total.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Edit: LOL I just re-read your title and that was 4 PHP and not PHP 4 :P my bad... :lol:

[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???
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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?
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Sorry, the line was

Code: Select all

private $m_database;
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:

Code: Select all

private $m_database = 0;
Which has not improved matters.

I'm struggling to see why the definition of a member is causing such problems.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post 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

Code: Select all

$database = new Database();
straight off, without first defining $database, is $database ever a T_VARIABLE?
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Got it.

I'm used to the 'this' operator in C++, but it works differently in PHP.

I should define it as

Code: Select all

private $m_database;
but then access it in the class as

Code: Select all

$this->m_database->function();
because the '$' on 'this' indicates the variable, I don't also need to indicate $m_database is a variable:

Code: Select all

$this->$m_database->function();
as I was doing in the code. Cheers for those who have helped.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Bingo! :) Glad you got it sorted...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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
Post Reply