php - Echo variable which has been set in another classes function -
i trying access contents of variable class. have code below, expecting 'test' returned, nothing.
i assume because getting $abc_rank
empty. required variable populated in function itself.
therefore how can $abc_rank
hold echo , output via other class?
<?php class class1 { public static $abc_rank; public function __construct() { $this->add_text(); } public function add_text() { $this->abc_rank = 'test'; } } class class2 { public function __construct() { $this->display(); } public function display() { $test = class1::$abc_rank; echo $test; } } $go = new class2(); ?>
i know can do:
public static $abc_rank = 'test';
but population of variable must in function.
i have read of other related answers , can't seem work.
in class1 :
replace $this->abc_rank = 'test';
$this::$abc_rank='test';
($abc_rank static property)
in class2 :
in display function : replace
$test = class1::$abc_rank; echo $test;
with
$test = new class1(); echo $test::$abc_rank;
(class1 isn't static)
full code here :
class class1 { public static $abc_rank; public function __construct() { $this->add_text(); } public function add_text() { //$this->abc_rank = 'test'; $this::$abc_rank='test'; } } class class2 { public function __construct() { $this->display(); } public function display() { //$test = class1::$abc_rank; //echo $test; $test = new class1(); echo $test::$abc_rank; } } $go = new class2();
Comments
Post a Comment