开发者

Javascript-like objects in PHP?

开发者_运维百科It's pretty handy in JS to create objects like this:

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

and then use them like:

test.foo.bar
test.bar2

Is there anything like this in PHP without declaring classes?


It's called associative arrays.

Example (note: the indentation is 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'
};

As an alternative, you can use the pre-declared StdClass.

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

which would 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 is the same as {} in Javascript)


stdClass allows you to create (essentially) typeless objects. For example:

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

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

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

Another method is to convert the associative array to an object afterwards with a recursive function. Here's 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 when you're not the one making the associative array.

Unfortunately, even though PHP 5.3 supports anonymous methods, you cannot put an anonymous method into a stdClass (though you can put one into an associative array). But this isn't too bad anyway; if you want functionality in it, you really should create a class instead.


You can use a StdClass object or an ArrayObject which are included in php (though the latter requires that you have SPL installed). Though unless you need to access the values specifically with the -> operator its more efficient to just use an associative array instead.


I think what you are looking for is an Associative Array

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

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


The closest thing would be arrays.

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

echo $test['foo']['bar'];


Technically, no. However if you are creating a data object (ie no methods), you could technically write a JSON string and use

$obj = json_decode($obj_string);

I wouldn't recommend it however. I assume there would be significant speed loss.

EDIT Though it goes without mentioning, associative arrays should be used for this instead of flat data objects.


The only reason to do that is if you wish to pass data back to a JavaScript function with JSON. In that case, use json_encode on the array. Otherwise, just keep it as an array, as there's not reason to encode it and then decode it just so it looks like JavaScript.


Try this way: https://github.com/ptrofimov/jslikeobject

Author implemented JS-like objects, you can even access properties from functions via $this pointer.

But perhaps it is not so good to use such objects instead of usual ones.


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜