« Back to Index

Add properties to PHP object dynamically

View original Gist on GitHub

object-properties.php

// Check it out online: http://phpepl.cloudcontrolled.com/
 
$obj = new stdClass();
$abc = 'something';
 
$obj->{$abc} = 'test'; // dynamically generated object key = MIND BLOWN
 
var_dump($obj); // => object(stdClass)#1 (1) { ["something"]=> string(4) "test" }