Because I'm lazy I was wondering if PHP has a shorthand way to set properties like this...
with $person_object {
->first_name = 'John';
->last_name = 'Smith';
->email = '[email protected]';
}
Is there anything like this? Or, is there a lazy way to set properties without having to type $person_object
over and over again?
You could implement something akin to the builder pattern within your Person
class. The approach involves returning $this
at the end of every setter call.
$person
->set_first_name('John')
->set_last_name('Smith')
->set_email('[email protected]');
And in your class...
class Person {
private $first_name;
...
public function set_first_name($first_name) {
$this->first_name = $first_name;
return $this;
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With