Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP write objects inline

Im starting to move away from using arrays in PHP as objects are so much neater and in php 5 there is no performance hits when using objects.

Currently the way I do it is:

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

However, i find it so tedious to have to typecast every time as typecasting isnt recursive...

Is there any way in php (or will there be) to do it like this or something similar:

$object = {
    'this' => 'that',
    'foo' => {
        'bar' => 123
    }
};
like image 702
user1821218 Avatar asked Nov 16 '12 12:11

user1821218


People also ask

Can you make objects in PHP?

PHP is something called an object-oriented programming language, which means that objects containing multiple different properties can be created.

What is a stdClass object?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.

Can we create object without class in PHP?

Using new stdClass() to create an object without class: For creating an object without a class, we will use a new stdClass() operator and then add some properties to them. Syntax: // Creating an object $object = new stdClass(); // Property added to the object $object->property = 'Property_value';

What is object data type in PHP?

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.


2 Answers

Starting from PHP 5.4 short array syntax has become available. This allows you to initialize array like this:

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

There is no currently short object syntax in PHP as of PHP7. But you can cast short array syntax to create objects like this:

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

Looks much nicer and shorter than using the following construct:

$myObject = new \StdClass();
$myObject->propertyA = 1;
$myObject->propertyB = 2;
like image 137
Ivan Yarych Avatar answered Sep 22 '22 22:09

Ivan Yarych


I have an alternative solution for when you receive an already built array. Say your array has n nested arrays so you have no chance of casting 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, as it is way slower than just sticking with the array sintax and live with it. Also, if an element of the array is a function or some other funny thing this would break that.

Example usage:

$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;
like image 21
santiago arizti Avatar answered Sep 21 '22 22:09

santiago arizti