Since I'm lazy, I was wondering if PHP has a short way to set such properties ...
with $person_object { ->first_name = 'John'; ->last_name = 'Smith'; ->email = ' spam_me@this-place.com '; }
Is there anything similar? Or is there a lazy way to set properties without retyping $person_object ?
$person_object
You can implement something similar to a builder pattern in your Person class. The approach involves returning $this at the end of each setter call.
Person
$this
$person ->set_first_name('John') ->set_last_name('Smith') ->set_email(' spam_me@this-place.com ');
And in your class ...
class Person { private $first_name; ... public function set_first_name($first_name) { $this->first_name = $first_name; return $this; } ... }
No.
No no.
A common decoration function can do this for you:
function decorate( $object, $data ) { foreach ( $data as $key => $value ) { $object->$key = $value; } } decorate( $person_object, array( 'first_name' => 'John', 'last_name' => 'Smith' ) );
I may have made some mistakes, it has been a while since I wrote the PHP code and this has not been verified
No, I do not think so. You could do such a thing:
class Person { function __call($method, $args) { if (substr($method, 0, 3) == set) { $var = substr($method, 3, strlen($method)-3); $this->$var = $args[0]; } else { //throw exception } } }
Source: https://habr.com/ru/post/1389522/More articles:C ++ const correctness with std :: pair - c ++https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1389518/how-to-push-components-to-edges-of-available-space-using-css-html&usg=ALkJrhi8vIfC7PsBRR-1Z0sZO82FB3BYXgProblems reading inputStream from Uri and sending via outputStream android - androidWhen it matters, do you use int int Int32 or string versus String? - c #Serial debugging of Windbg not working through virtual serial port - windbgHow to pass a query string parameter? - jsf-2Regular expression to check for duplicate digits - javaJustification for Java exceptions - javaThe memory does not appear in the hierarchy of the ios view - iosCustom UITableViewCell is broken when dragged to reorder - iosAll Articles