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

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
nomad
Forum Newbie
Posts: 3
Joined: Sat Feb 26, 2011 3:07 pm

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

Post 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();
LordXzi
Forum Newbie
Posts: 7
Joined: Sat Feb 26, 2011 5:29 pm
Location: Reading, UK

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

Post 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);
nomad
Forum Newbie
Posts: 3
Joined: Sat Feb 26, 2011 3:07 pm

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

Post 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
nomad
Forum Newbie
Posts: 3
Joined: Sat Feb 26, 2011 3:07 pm

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

Post 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.
LordXzi
Forum Newbie
Posts: 7
Joined: Sat Feb 26, 2011 5:29 pm
Location: Reading, UK

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

Post 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; }
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

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

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