This has to be simple, but I can't seem to find an answer....
I have a generic stdClass object $foo
with no properties. I want to add a new property $bar
to it that's not already defined. If I do this:
$foo = new StdClass[];
$foo->bar = '1234';
PHP in strict mode complains.
What is the proper way [outside of the class declaration] to add a property to an already instantiated object?
NOTE: I want the solution to work with the generic PHP object of type stdClass.
A little background on this issue. I'm decoding a json string which is an array of json objects. json_decode[]
generates an array of StdClass object. I need to manipulate these objects and add a property to each one.
hakre
187k48 gold badges419 silver badges804 bronze badges
asked Jul 23, 2012 at 18:32
4
If you absolutely have to add the property to the object, I believe you could cast it as an array, add your property [as a new array key], then cast it back as an object. The only time you run into stdClass
objects [I believe] is when you cast an array as an object or when you create a new stdClass
object from scratch [and of course when you json_decode[]
something - silly me for forgetting!].
Instead of:
$foo = new StdClass[];
$foo->bar = '1234';
You'd do:
$foo = array['bar' => '1234'];
$foo = [object]$foo;
Or if you already had an existing stdClass object:
$foo = [array]$foo;
$foo['bar'] = '1234';
$foo = [object]$foo;
Also as a 1 liner:
$foo = [object] array_merge[ [array]$foo, array[ 'bar' => '1234' ] ];
answered Jul 23, 2012 at 18:45
WWWWWW
9,4641 gold badge28 silver badges32 bronze badges
6
Do it like this:
$foo = new stdClass[];
$foo->{"bar"} = '1234';
now try:
echo $foo->bar; // should display 1234
answered Sep 15, 2015 at 8:55
JM R.JM R.
1,5311 gold badge9 silver badges4 bronze badges
7
If you want to edit the decoded JSON, try getting it as an associative array instead of an array of objects.
$data = json_decode[$json, TRUE];
answered Jul 23, 2012 at 18:57
gen_Ericgen_Eric
218k40 gold badges295 silver badges334 bronze badges
This is another way:
$foo = [object]null; //create an empty object
$foo->bar = "12345";
echo $foo->bar; //12345
answered Aug 17, 2016 at 18:18
RagnarRagnar
4,1431 gold badge25 silver badges39 bronze badges
you should use magic methods __Set and __get. Simple example:
class Foo
{
//This array stores your properties
private $content = array[];
public function __set[$key, $value]
{
//Perform data validation here before inserting data
$this->content[$key] = $value;
return $this;
}
public function __get[$value]
{ //You might want to check that the data exists here
return $this->$content[$value];
}
}
Of course, don't use this example as this : no security at all :]
EDIT : seen your comments, here could be an alternative based on reflection and a decorator :
class Foo
{
private $content = array[];
private $stdInstance;
public function __construct[$stdInstance]
{
$this->stdInstance = $stdInstance;
}
public function __set[$key, $value]
{
//Reflection for the stdClass object
$ref = new ReflectionClass[$this->stdInstance];
//Fetch the props of the object
$props = $ref->getProperties[];
if [in_array[$key, $props]] {
$this->stdInstance->$key = $value;
} else {
$this->content[$key] = $value;
}
return $this;
}
public function __get[$value]
{
//Search first your array as it is faster than using reflection
if [array_key_exists[$value, $this->content]]
{
return $this->content[$value];
} else {
$ref = new ReflectionClass[$this->stdInstance];
//Fetch the props of the object
$props = $ref->getProperties[];
if [in_array[$value, $props]] {
return $this->stdInstance->$value;
} else {
throw new \Exception['No prop in here...'];
}
}
}
}
PS : I didn't test my code, just the general idea...
answered Jul 23, 2012 at 18:37
2
I don't know whether its the newer version of php, but this works. I'm using php 5.6