Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 494
mvbl fst Avatar asked Dec 09 '10 18:12

mvbl fst


People also ask

Are there PHP objects?

In PHP, Object is a compound data type (along with arrays). Values of more than one types can be stored together in a single variable. Object is an instance of either a built-in or user defined class. In addition to properties, class defines functionality associated with data.

What is EMP in JavaScript?

Here Emp can act as a class in JavaScript. The body of Emp acts as a constructor and is called as soon as we create an object of the class. Creating Objects of Emp Class. Using the following syntax we can create an object of the Emp class: var Emp1 = new Emp();


3 Answers

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)

like image 130
Lekensteyn Avatar answered Oct 18 '22 14:10

Lekensteyn


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.

like image 39
Jonah Avatar answered Oct 18 '22 14:10

Jonah


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.

like image 3
prodigitalson Avatar answered Oct 18 '22 16:10

prodigitalson