Answer the question
In order to leave comments, you need to log in
How to properly initialize a multidimensional stdClass object?
Good afternoon.
Dealing with PHP objects, I also came across the possibility of creating dynamic objects using the predefined class stdClass():
I also found the following example of dynamic object initialization:$std = new stdClass();
$std = new stdClass();
$std->first = 1;
$std->second->third = 3;
print_R($std);
?>
stdClass Object
(
[first] => 1
[second] => stdClass Object
(
[third] => 3
)
)
Creating default object from empty value
$std->second->third = 3;
Answer the question
In order to leave comments, you need to log in
In fact, you do not get an error, but a Warning, and this is how it should be, because. this breaks the logic of applications and OOP in particular. It is more correct to always set a structure and inherit it, at least in the future, when the moment of code refactoring comes, you can always find the necessary properties and extend them.
And a working example with the error level turned off:
<?php
error_reporting(error_reporting() & ~E_WARNING);
$std = new stdClass();
$std->first = new stdClass();
$std->first->third = 3;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question