Assuming your goal is as follows:
print $field->textfield->value;
if you want this to work:
$field = new Field; $field->setValue('textfield', 'something to print')->setLabel('Label here');
Then you need to do something like this:
class FieldProperty { public $label public $value; public setValue($value) { $this->value = $value; return $this; } public setLabel($label) { $this->label = $label; return $this; } } class Field { public function setValue($property, $value) { $this->$property = new FieldProperty(); $this->$property->setValue($value); return $this->$property; } }
Thinking about this further, we can add:
class Field { public function setValue($property, $value) { $this->$property = new FieldProperty(); $this->$property->setValue($value); return $this->$property; } public function setProperty($propertyName) { $this->$propertyName = new FieldProperty; return $this->$propertyName; } }
Now you can do this:
$field->setProperty('textfield')->setLabel('my label')->setValue('some value');
source share