I am new to PHP OOP and I am having problem getting arrays back.
class example
{
public $array;
public function __construct()
{
$this->array = array();
}
public function do_work()
{
$this->array[] = 'test';
}
}
$test = new example();
$test->do_work();
$test->array;
I keep getting a empty array instead of ‘test’.
What am I doing wrong?
This is because you never actually call the function
$test->do_work();The constructor just creates the empty array, and then you attempt to access the property. It should be empty.Updates
I see you updated your question. If you simply
echo $test->array, it should just printArray. However, when I copy your updated code and perform avar_dump($test->array), this is the output I get:Which I believe is what you are expecting. The code that you have in your question, though, should output nothing. You are doing nothing with
$test->array, the variable is being evaluated and then thrown away.