M
M
Mike Evstropov2016-01-12 13:23:52
PHP
Mike Evstropov, 2016-01-12 13:23:52

What would be the equivalent of a javascript object in PHP?

var obj 	= {
    'a' : function(){ return 1; },
    'b' : {
      'ba' : function(){ return 2; },
      'bb' : {
        'bba' : function(){ return 3; }
      }
    }
  }
  obj.a();
  obj.b.ba();
  obj.b.bb.bba();

Please give an example of this js code in php.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cat Anton, 2016-01-12
@Aroused

$obj = (object) [
    'a' => function(){ return 1; },
    'b' => (object) [
        'ba' => function(){ return 2; },
        'bb' => (object) [
            'bba' => function(){ return 3; }
        ]
    ]
];
call_user_func($obj->a);
call_user_func($obj->b->ba);
call_user_func($obj->b->bb->bba);

ideone.com/V0VcC8
You can also do this:
$obj->a->__invoke();
$obj->b->ba->__invoke();
$obj->b->bb->bba->__invoke();

X
xmoonlight, 2016-01-12
@xmoonlight

class

A
Alexey Ukolov, 2016-01-12
@alexey-m-ukolov

If on the knee, then like this :

$obj = [
    'a' => function(){ return 1; },
    'b' => [
      'ba' => function(){ return 2; },
      'bb' => [
        'bba' => function(){ return 3; }
      ]
    ]
];

$obj['a']();
$obj['b']['ba']();
$obj['b']['bb']['bba']();

In general, of course, xmoonlight is right.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question