Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve the missing object properties in PHP?

This is a bit philosophical but I think many people encountered this problem. The goal is to access various (dynamically declared) properties in PHP and get rid of notices when they are not set.

Why not to __get?
That's good option if you can declare your own class, but not in case of stdClass, SimpleXML or similar. Extending them is not and option since you usually do not instantiate these classes directly, they are returned as a result of JSON/XML parsing.

Example:

$data = '{"name": "Pavel", "job": "programmer"}';
$object = json_decode($data);

We have simple stdClass object. The problems is obvious:

$b = $data->birthday;

The property is not defined and therefore a notice is raised:

PHP Notice:  Undefined property: stdClass::$birthday

This can happen very often if you consider that you get that object from parsing some JSON. The naive solution is obvious:

$b = isset($data->birthday) ? $data->birthday : null;

However, one gets tired very soon when wrapping every accessor into this. Especially when chaining the objects, such as $data->people[0]->birthday->year. Check whether people is set. Check if the first element is set. Check if birthday is set. Check if year is set. I feel a bit overchecked...

Question: Finally, my question is here.
What is the best approach to this issue? Silencing notices does not seem to be the best idea. And checking every property is difficult. I have seen some solutions such as Symfony property access but I think it is still too much boilerplate. Is there any simpler way? Either third party library, PHP setting, C extension, I don't care as far as it works... And what are the possible pitfalls?

like image 398
Pavel S. Avatar asked Aug 21 '13 15:08

Pavel S.


People also ask

How do I get the properties of an object in PHP?

The get_object_vars() function is an inbuilt function in PHP that is used to get the properties of the given object. When an object is made, it has some properties. An associative array of properties of the mentioned object is returned by the function. But if there is no property of the object, then it returns NULL.

What are the properties of object in PHP?

Properties of Object Properties are variables that are defined within a class. These variables are then used by the methods, objects of the class. These variables can be public, protected or private. By default, the public is used.

How do I remove a property from a PHP object?

How do you delete an object in PHP? An object is an instance of a class. Using the PHP unset() function, we can delete an object. So with the PHP unset() function, putting the object that we want to delete as the parameter to this function, we can delete this object.

What are the properties in PHP code?

Data members declared inside class are called properties. Property is sometimes referred to as attribute or field. In PHP, a property is qualified by one of the access specifier keywords, public, private or protected. Name of property could be any valid label in PHP.


1 Answers

in PHP version 8

you can use Nullsafe operator as follow:

$res = $data?->people[0]?->birthday?->year;
like image 97
Ali_Hr Avatar answered Sep 19 '22 09:09

Ali_Hr