Javascript-like objects in PHP?

I have a question. It is very convenient in JS to create such objects:

test = { foo : { bar : "hello world" }, bar2 : "hello world 2" } 

and then use them like

 test.foo.bar test.bar2 

Is there anything similar in PHP without declaring classes?

+7
source share
9 answers

stdClass allows you to create (essentially) faceless objects. For instance:

 $object = (object) array( 'name' => 'Trevor', 'age' => 42 ); 

As shown here, the fastest way to create an stdClass object is to create an associative array. For multiple levels, you again do the same inside:

 $object = (object) array( 'name' => 'Trevor', 'age' => '42', 'car' => (object) array( 'make' => 'Mini Cooper', 'model' => 'S', 'year' => 2010 ) ); 

Another method is to convert an associative array to an object followed by a recursive function. Here is an example.

 function toObject(array $array) { $array = (object) $array; foreach ($array as &$value) if (is_array($value)) $value = toObject($value); return $array; } // usage: $array = // some big hierarchical associative array... $array = toObject($array); 

This is useful if you are not an associative array.

Unfortunately, although PHP 5.3 supports anonymous methods, you cannot put an anonymous method in stdClass (although you can put it in an associative array). But this is not so bad; if you need functionality, you really need to create a class.

+9
source

It is called associative arrays.

Example (note: indent for layout purposes):

 $test = array( 'foo' => array( 'bar' => 'hello world' ), 'bar2' => 'hello world 2' ); $test['foo']['bar']; $test['bar2']; 

This is equivalent to the following Javascript code:

 var test = { 'foo': { 'bar': 'hello world', }, 'bar2': 'hello world 2' }; 

Alternatively, you can use the previously declared StdClass.

 $test = new StdClass; $test->foo = new StdClass; $test->foo->bar = 'hello world'; $test->bar2 = 'hello world 2'; 

which will be written in JavaScript as:

 var test = new Object; test.foo = new Object; test.foo.bar = 'hello world'; test.bar2 = 'hello world 2'; 

(note: new Object same as {} in Javascript)

+8
source

You can use a StdClass or ArrayObject, which are included in php (although the latter requires that you have SPL installed). Although, if you do not need to access values ​​specifically using the -> operator, it is more efficient to use an associative array instead.

+2
source

I think you're looking for an Associative Array

 $test["foo"]["bar"] 

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=keyed+arrays

0
source

The closest thing is arrays.

 $test = array( 'foo' => array('bar' => 'hello world'), 'bar2' => 'hello world 2', ); echo $test['foo']['bar']; 
0
source

Technically, no. However, if you create a data object (i.e. without methods), you can technically write a JSON string and use

 $obj = json_decode($obj_string); 

I would not recommend it. I suppose there will be a significant loss of speed.

EDIT Although not mentioned, associative arrays should be used instead of local data objects.

0
source

The only reason for this is to pass data back to JavaScript functions using JSON. In this case, use json_encode in the array. Otherwise, just save it as an array, since there is no reason to encode it and then decode it so that it looks like JavaScript.

0
source

Try as follows: https://github.com/ptrofimov/jslikeobject

The author has implemented JS-like objects, you can even access properties from functions via the $ this pointer.

But it may not be so good to use such objects instead of ordinary ones.

0
source
 $a = array( 'a'=> 123, 'b'=> 334, 'c'=> 7853 ); echo json_encode($a); 

This will be the result: {"a":123,"b":334,"c":7853}

0
source

All Articles