PHP -> read object value

Status
Not open for further replies.

Porsche_maniak

Active Member
283
2009
0
40
Hello guys,
I've object that I can var_dump and see its contents that way but I cannot understand how to read the values the right way.

This is the var_dump to the variable called $container and i got this:
[
Code:
myobject Object (    [[B]id[/B]:myobject:private] => 56644   [[B]content[/B]:myobject:private] => thecontent
.......... and so on

How do I print the "id" value guys?
I tried
echo $container->id or $container["id"] , which is obviously far from the truth.

Any help?
Thanks
 
2 comments
[FONT=proxima_nova_rgregular]Dont worry.
First of all: "id" is private. Meaning you can ONLY access it inside the container class. You need to make it public if you want to access it like that.

Just replace this

[/FONT]

[FONT=proxima_nova_rgregular]
PHP:
private $id;
[/FONT]


[FONT=proxima_nova_rgregular]with [/FONT][FONT=proxima_nova_rgregular]
PHP:
public $id;
[/FONT]
[FONT=proxima_nova_rgregular]

[/FONT]
[FONT=proxima_nova_rgregular]then you can do echo
PHP:
$container->id
;[/FONT]
[FONT=proxima_nova_rgregular]

Otherwise you need to add a so called "getter" to your container class or even a "setter" if you want to change Id later on

PHP:
//getter
public fuction getId() {
  return $this->id;
}

//setter
public function setId($id) {
  $this->id = $id;
}

Then you need to do
PHP:
$container->getId()


I hope that helps.



[/FONT]
 
Status
Not open for further replies.
Back
Top