Pass variables from one OOP class to another?
Posted: Tue May 26, 2009 11:18 am
Hello all,
I'm new to OOP PHP and I'm trying to pass the "Name" variable from my first class to my second class. Can anyone tell me what I'm doing wrong?
Thanks!
I'm new to OOP PHP and I'm trying to pass the "Name" variable from my first class to my second class. Can anyone tell me what I'm doing wrong?
Code: Select all
<?php
//construct our class
class Demo {
//assign a variable
public $name;
//construct our method
function sayHello(){
print "Hello $this->name!";
}
}
class Test extends Demo{
function screamHello(){
Demo::sayHello($this->name);
}
}
//instantiate our class
$objDemo = new Demo();
//assign what the name variable is in our class
$objDemo->name = 'Bob';
//to access the method, we have to use the same var name as our instantiated class
$objDemo->sayHello();
$objDemo = new Test();
$objDemo->screamHello();
?>