mvbl fst
mvbl fst

Reputation: 5263

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?

Upvotes: 17

Views: 13524

Answers (9)

Baddam Pranayteja
Baddam Pranayteja

Reputation: 1

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

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

Upvotes: 0

Renato Cuccinotto
Renato Cuccinotto

Reputation: 59

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.

Upvotes: 0

Jonah
Jonah

Reputation: 10091

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.

Upvotes: 16

Brian H
Brian H

Reputation: 833

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.

Upvotes: 0

Lekensteyn
Lekensteyn

Reputation: 66465

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)

Upvotes: 21

Craige
Craige

Reputation: 2891

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.

Upvotes: 0

simshaun
simshaun

Reputation: 21466

The closest thing would be arrays.

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

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

Upvotes: 0

Dutchie432
Dutchie432

Reputation: 29160

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

Upvotes: 0

prodigitalson
prodigitalson

Reputation: 60413

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.

Upvotes: 3

Related Questions