Page 1 of 1

How do you access variable in Class A from Class B?

Posted: Sat Feb 26, 2011 3:17 pm
by nomad
I am using PHP5 and experimenting in using classes for the first time. My first attempt is pretty basic but yet kicking my butt. I define 2 classes. The first class defines a single variable. The second class has 3 functions. One function requires reading the variable from the first class. How do you do that? Here is an example of my code.

// member.php

Code: Select all

class member {

var $SignedIn;

function __construct()
{
// $this->SignedIn = 0; // test to indicate user is not signed in
$this->SignedIn = 1; // test to indicate user is signed in
}

}
// page.php

Code: Select all

class page {
{

function DisplayHeader()
{
// code goes here
}

function DisplayMenu()
{
// determines which links to display in the menu
if(member::SignedIn == 1) { echo "member signed in"; } else { echo "guest not signed in"; }
}

function DisplayFooter()
{
// code goes here
}

}
// index.php

Code: Select all

require "member.php";
require "page.php";

my_member = new member;
my_page = new page;

my_page->DisplayHeader();

my_page->DisplayMenu();

my_page->DisplayFooter();

Re: How do you access variable in Class A from Class B?

Posted: Sat Feb 26, 2011 5:59 pm
by LordXzi
I would recommend that you define what is called an 'accessor' within your member class, this is generally good object oriented practice:

Code: Select all

function getSignedIn { return $this->SignedIn; }
Then within your if statement in the page class you can use the function

Code: Select all

function DisplayMenu($member) {
if($member->getSignedIn( ) == 1) { etc }
}
In your index.php file then you need only give the member to the call to DisplayMenu so it has access

Code: Select all

my_page->DisplayMenu(my_member);

Re: How do you access variable in Class A from Class B?

Posted: Sat Feb 26, 2011 8:37 pm
by nomad
I tried what you suggested and received the following error message.

Fatal error: Call to a member function getSignedIn() on a non-object in /var/www/page.php on line 16

Re: How do you access variable in Class A from Class B?

Posted: Sat Feb 26, 2011 9:01 pm
by nomad
I got it to work. I had to add the $ to my_page->DisplayMenu(my_member);. It should be $my_page->DisplayMenu($my_member);

Thank you for the idea and help. I will read up on 'accessor'.

I won't be a newbie forever.

Re: How do you access variable in Class A from Class B?

Posted: Sun Feb 27, 2011 6:51 am
by LordXzi
Glad I could help and sorry for missing your mistake!

If you're learning how to do classes I would recommend setting the variable that you passed into the function as a 'member variable' so you can store it for use later. That way if your page class needs to refer to the current member frequently you need not pass it as an argument every time.

All you need to add is the following to your page class, and create a mutator to set it initially.

Code: Select all

var $member;
function setMember($newMember) { $this->member = $newMember; }

Re: How do you access variable in Class A from Class B?

Posted: Sun Feb 27, 2011 9:29 am
by DigitalMind
nomad wrote:I am using PHP5

Code: Select all

var
I don't believe you :wink:
Use public, protected or private instead of var.