0

I'm trying to get the ['text'] value from an array object.
When I try to print_r($item), I get this as output:

ToDo Object
(
[data:private] => Array
    (
        [id] => 128
        [user_id] => 6785
        [view_stat] => 0
        [position] => 12
        [text] => 3rd try
        [dt_added] => 2012-07-17 04:29:08
        [tick] => 0
        [temp_view] => 6785
        [viewer] => 6785
    )
)

how to get the [text] value in php?? thanks

2
  • 2
    Use the methods that the ToDo object has... it should provide an interface to access this information. Commented Jul 19, 2012 at 11:09
  • print_r(get_class_methods($item)) to see if there is a public method to get what you want. Commented Jul 19, 2012 at 11:09

1 Answer 1

4

Since those are private variables, you can't!

You need to create a public function inside the class, that will return the specific data needed:

public function getText () {
  return $this -> text;
}

And outside the class you can retrieve it like this:

$class = new ToDo();
$myText = $class -> getText();
Sign up to request clarification or add additional context in comments.

4 Comments

if the problem is just the private thing, can I just change the private var to public? how to access it? I really don't know the syntax. thanks!
I just wrote you the full syntax. Paste 1st code into your ToDo class and the second one outside, where you need to access it.
Yes. You can simply change from private to public and then access it as $class->data->text;
Of course, but I wouldn't suggest that. It's better to keep variables private and make public functions that output the variables. You get more control over the data given.

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.