Skip to main content
improved formatting
Source Link

This is the only two ways I found to achieve it

#1 Using get_object_vars

You can use get_object_vars that returns all public variables that a specific object contains, then you can iterate them using foreach

example:

if (self::$instance !== null) { foreach (get_object_vars(self::$instance) as $prop=>$value) { $this->{$prop} = $value; } } 

#2 Is using PHP reflection, I find it less useful in my case because it doesn't reflect properties that was generated on the go

Note: Reflection will reflect also properties such as private|static and so. you can filter them inside the getProperties function or iterate through all properties and use boolean methods filter between them.

example:

if (self::$instance !== null) { $reflection = new ReflectionClass(self::$instance); $props = $reflection->getProperties(); foreach ($props as $prop) { $this->{$prop->name} = $prop->getValue(self::$instance); } } 

so if you do self::$instance->newVar = 1; it won't be reflected. but in the #1 Option it will be.

Hope it will help someone out there.

This is the only two ways I found to achieve it

#1 Using get_object_vars

You can use get_object_vars that returns all variables that a specific object contains, then you can iterate them using foreach

example:

if (self::$instance !== null) { foreach (get_object_vars(self::$instance) as $prop=>$value) { $this->{$prop} = $value; } } 

#2 Is using PHP reflection, I find it less useful because it doesn't reflect properties that was generated on the go

example:

if (self::$instance !== null) { $reflection = new ReflectionClass(self::$instance); $props = $reflection->getProperties(); foreach ($props as $prop) { $this->{$prop->name} = $prop->getValue(self::$instance); } } 

so if you do self::$instance->newVar = 1; it won't be reflected. but in the #1 Option it will be.

Hope it will help someone out there.

This is the only two ways I found to achieve it

#1 Using get_object_vars

You can use get_object_vars that returns all public variables that a specific object contains, then you can iterate them using foreach

example:

if (self::$instance !== null) { foreach (get_object_vars(self::$instance) as $prop=>$value) { $this->{$prop} = $value; } } 

#2 Is using PHP reflection, I find it less useful in my case because it doesn't reflect properties that was generated on the go

Note: Reflection will reflect also properties such as private|static and so. you can filter them inside the getProperties function or iterate through all properties and use boolean methods filter between them.

example:

if (self::$instance !== null) { $reflection = new ReflectionClass(self::$instance); $props = $reflection->getProperties(); foreach ($props as $prop) { $this->{$prop->name} = $prop->getValue(self::$instance); } } 

so if you do self::$instance->newVar = 1; it won't be reflected. but in the #1 Option it will be.

Hope it will help someone out there.

Source Link

This is the only two ways I found to achieve it

#1 Using get_object_vars

You can use get_object_vars that returns all variables that a specific object contains, then you can iterate them using foreach

example:

if (self::$instance !== null) { foreach (get_object_vars(self::$instance) as $prop=>$value) { $this->{$prop} = $value; } } 

#2 Is using PHP reflection, I find it less useful because it doesn't reflect properties that was generated on the go

example:

if (self::$instance !== null) { $reflection = new ReflectionClass(self::$instance); $props = $reflection->getProperties(); foreach ($props as $prop) { $this->{$prop->name} = $prop->getValue(self::$instance); } } 

so if you do self::$instance->newVar = 1; it won't be reflected. but in the #1 Option it will be.

Hope it will help someone out there.