8

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.

1
  • 2
    Please post the actual class definition. Commented Aug 25, 2011 at 20:34

4 Answers 4

8

You mentioned that $value is 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 $value just like any other array:

$object = new classname;
$name = $object->value["name"];
$age = $object->value["age"];

// Or you can use foreach, getting both key and value
foreach ($object->value as $key => $value) {
    echo $key . ": " . $value;
}

However, if you're talking about declaring public $value in a function then that's a syntax error.

Furthermore if you declare $value (within a function) without the public modifier then its scope is limited to that function and it cannot be public. 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.

Sign up to request clarification or add additional context in comments.

Comments

2

The same as you would normally use an array.

$getValue = new yourClass();
$getValue->value['name'];

3 Comments

He also said $value is a public variable inside his class, therefore accessible.
@Rob I get that, but he said class so therefore class abc { public $var; $this->var['test'] = '1'; } $object = new abc(); echo $object->var['test']; //Outputs 1
thanks to both of you. @rob, sorry I din't get you about editing the question and vote.
1

Use code

foreach($getValue->value as $key=>$value)

Comments

0
    <?php
        interface Nameable {
            public function getName($i);
            public function setName($a,$name);
        }
        class Book implements Nameable {
            private $name=array();
            public function getName($i) {
                return $this->name[$i];
            }
            public function setName($i, $name) {
                return $this->name[$i] = $name;
            }
        }

        $interfaces = class_implements('Book');

        if (isset($interfaces['Nameable'])) {
            $bk1 = new Book;
            $books = array('bk1', 'bk2', 'bk3', 'bk4', 'bk5');
            for ($i = 0; $i < count($books); $i++)
                $bk1->setName($i, $books[$i]);
            for ($i = 0; $i < count($books); $i++)
                echo '// Book implements Nameable: ' . $bk1->getName($i) . nl();
        }
?>

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.