PHP write inline objects

I am starting to move away from using arrays in PHP, since objects are so neat and in php 5 there is no performance when using objects.

I am currently doing this:

$object = (object) array('this' => 'that', 'foo' => (object) array('bar' => 123));

However, I find it so tiring to come up with a type every time, since typecasting is not recursive ...

Is there any way in php (or will be) for this, like this or something similar:

 $object = { 'this' => 'that', 'foo' => { 'bar' => 123 } }; 
+7
source share
6 answers

Starting with PHP 5.4, the syntax of short arrays has appeared. This allows you to initialize the array as follows:

 $myArray = ["propertyA" => 1, "propertyB" => 2]; 

PHP lacks the current syntax of PHP objects with PHP7. But you can use short array syntax to create such objects:

 $myObject = (object) ["propertyA" => 1, "propertyB" => 2]; 

It looks much prettier and shorter than using the following design:

 $myObject = new \StdClass(); $myObject->propertyA = 1; $myObject->propertyB = 2; 
+16
source

I recommend that you build the width of the StdClass function.

 function arrayToObject($array) { if(!is_array($array)) { return $array; } $object = new stdClass(); if (is_array($array) && count($array) > 0) { foreach ($array as $name=>$value) { $name = strtolower(trim($name)); if (!empty($name)) { $object->$name = arrayToObject($value); } } return $object; } else { return FALSE; } } 
+4
source

PHP currently does not support short object syntax. Starting with PHP 5.4, they support short array syntax. Perhaps 5.5 will include what you need.

As an alternative:

You can create your objects as JSON and use json_decode() :

 $json = '{"this": "that", "foo": {"bar": 123}}'; var_dump(json_decode($json)); 

Note: I show this only as a demonstration of how to resolve your issue. I am not a supporter of this practice.

+2
source

Unfortunately, there is no syntax creating such instances of stdClass . But you can use the new keyword and any of the spl classes (not that clearer). If you want to optimize keystrokes, you can write a small helper function as follows:

 function O($a){ return new ArrayObject($a); // has -> and [] support too } 

and write

 $stuff = O(array('a' => 'tickle me elmo', 'b' => O(array('foo' => 'bar')))); 
0
source

Although I honestly do not understand why you want to do this (associative arrays are essentially data-only objects), but if you insist:
Instead of casting each individual array at each level to an object, you can use the following trick / hack:

 $object = json_decode( json_encode( array('some'=>array('multi'=>'Dimensional'), 'array'=>'that', 'you' => array('want' => 'to', 'turn' => 'into'), 'an' => 'object'))); 

This will convert all arrays to stdClass instances, which I believe is what you wanted.
Again, I have to say: PHP is not JavaScript, and objects are much more expensive (relatively speaking) in languages ​​like PHP, then they are in JS. I highly recommend you use files with arrays if you don't need an object.

Like objects, arrays can be entered with types: function foo (array $argument){}
If you really want to turn them into a concrete instance of a class, why not change the constructor to work with an array:

 class My_Object extends stdClass { public function __construct(array $params = null) { if (!empty($params)) { foreach ($params as $name => $value) { $this->{$name} = $value;//set everything } } } } 

And add all the methods you want to add to the lot

0
source

I have an alternative solution when you get an already constructed array. Say your array has n nested arrays, so you have no chance to sketch each one in a simple way. This would do the trick:

 $object = json_decode(json_encode($unknownArray)); 

I would not use this in a large loop or something similar, since it is slower than just sticking to the sintax array and living with it. Also, if an array element is a function or some other fun thing, it will break it.

Usage example:

 $unknownArray = array( 'a' => '1', 'b' => '2', 'c' => '3', 'd' => array( 'x' => '7', 'y' => '8', 'z' => '9', ), ); $object = json_decode(json_encode($unknownArray)); echo $object->d->x; 
0
source

All Articles