0

so i'm having a php class like the following:

class myClass
{
    function __construct()
    {
        $this->chart_data = array(1,2,3,4,5);
        $this->captions   = array("a", "b", "c", "d", "e");
    }
}

is there a way to access the properties the same way like an associative array, like:

$obj = new myClass();
echo $obj['chart_data'];
echo $obj['captions'];

thanks!

3

2 Answers 2

1

you can try

echo $obj->{'chart_data'};
echo $obj->{'captions'};
Sign up to request clarification or add additional context in comments.

1 Comment

Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
0

From this answer you can try like this

$obj = (array) new myClass();
print_r($obj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.