Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting data from PHP Object with Colons and Slashes in Name

Tags:

php

Im new to Objects and have some basic understanding, but struggling with getting data from a particular type of node.

This:

$test->broadcast_data

Returns:

object(threewp_broadcast\BroadcastData)#1599 (5) {
  ["id"]=>
  int(49663)
  ["blog_id"]=>
  int(1)
  ["post_id"]=>
  int(38863)
  ["dataModified":"threewp_broadcast\broadcast_data":private]=>
  bool(true)
  ["data":"threewp_broadcast\broadcast_data":private]=>
  array(2) {
    ["version"]=>
    int(2)
    ["linked_children"]=>
    array(3) {
      [2]=>
      int(18557)
      [3]=>
      int(8097)
      [4]=>
      int(1768)
    }
  }
}

I know i can get the ID by doing:

$test->broadcast_data->id

But how do i get the linked_children array and assign it to a variable. The colon and slashes in "data":"threewp_broadcast\broadcast_data":private is throwing me off.

Thanks

like image 738
Joe Avatar asked Feb 07 '18 19:02

Joe


Video Answer


1 Answers

The name of the property is data. The colons and slashes that you're seeing is not part of the property of the class.

"data":"threewp_broadcast\broadcast_data":private

The threewp_broadcast\broadcast_data represents the namespace and class that the property belongs to and the :private means that the data property is private, therefore you cannot access it without a class method.

You can look in the class file to see if there's a function like getData() which would return the value of the private property.

For example, a class like the following:

namespace A;

class B{
    private $test;   
}

And the dump of it would give the following:

object(A\B)#1 (1) {
  ["test":"A\B":private]=>
  NULL
}

As you can see "test":"A\B":private is not the name of the property.

Update

After looking in the class threewp_broadcast\broadcast_data, there is a function named getData() so you could do:

$test->broadcast_data->getData()['linked_children'];

Or simply use the function get_linked_children() provided by the class:

$test->broadcast_data->get_linked_children();
like image 76
Chin Leung Avatar answered Oct 03 '22 21:10

Chin Leung