I have a array like this in a function:
$value = array("name"=>"test", "age"=>"00");
I made this $value as public inside the class abc.
Now in my other file, I want to access the values from this array, so I create an instance by:
$getValue = new <classname>;
$getValue->value..
I’m not sure how to proceed so that then I can access each element from that array.
You mentioned that
$valueis in a function, but is public. Can you post the function, or clarify whether you meant declaring or instantiating within a function?If you’re instantiating it that’s perfectly fine, and you can use the array keys to index
$valuejust like any other array:However, if you’re talking about declaring
public $valuein a function then that’s a syntax error.Furthermore if you declare
$value(within a function) without thepublicmodifier then its scope is limited to that function and it cannot bepublic. The array will go out of scope at the end of the function and for all intents and purposes cease to exist.If this part seems confusing I recommend reading up on visibility in PHP.