How can I create a property from a given argument inside a object\'s method?
class Foo{
public function createProperty($var_name, $val){
// here how c
Use the syntax: $object->{$property} where $property is a string variable and $object can be this if it is inside the class or any instance object
Live example: http://sandbox.onlinephpfunctions.com/code/108f0ca2bef5cf4af8225d6a6ff11dfd0741757f
class Test{
public function createProperty($propertyName, $propertyValue){
$this->{$propertyName} = $propertyValue;
}
}
$test = new Test();
$test->createProperty('property1', '50');
echo $test->property1;
Result: 50