How do you access variable in Class A from Class B?
Posted: Sat Feb 26, 2011 3:17 pm
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
// page.php
// index.php
// 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
}
}
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
}
}
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();