Reputation: 3
okey so here is my structure.
one.php two.php three.php
one.php includes both two.php and three.php
two.php is
class two {
function test(){ $var ='gello'; }}
three.php is
class three {
function testt(){ $var ='hello'; }}
So how could i use the $var variable of two.php in three.php ?
in one.php i can do that by
$one = new two();
$one->var;
any help would be appreciated.
Thanks
Upvotes: 0
Views: 71
Reputation: 4974
You need to define variable outiside the function
When you write inside function, only the function know who are $var
and show te correct value.
class two {
public $var = 'foo';
function setVar($var = 'foo') {
$this->var = $var;
}
}
class three {
function test() {
$two = new two();
echo($two->var); // Show 'foo'
$two->setVar('bar');
echo($two->var); // Show 'bar'
}
}
// Result 'foo'
$one = new two();
echo($one->var);
// Result 'fooz'
$one->setVar('fooz');
echo($one->var);
// Result 'foo' and 'bar'
$three = new three();
$three->test();
Upvotes: 1