PHP – create class properties at runtime

How to dynamically create  in PHP a property for an object?

If the object¬† is $foo you use this syntax: $this->{$property} = ‘a value’;

For example:

<?php

class foo {

public function setProperty($n, $v)
{
   $this->{$n} = $v;
}

}

$foo = new foo();
$foo->setProperty('property1','value1');
echo $foo->property1; //will display value1
?>