是否有设置许多对象属性的简写方法?

noc*_*ber 4 php

因为我很懒,我想知道PHP是否有简写方法来设置这样的属性......

with $person_object {
    ->first_name = 'John';
    ->last_name = 'Smith';
    ->email = 'spam_me@this-place.com';
}
Run Code Online (Sandbox Code Playgroud)

有这样的事吗?或者,是否有一种懒惰的方式来设置属性而无需$person_object反复输入?

Fin*_*arr 8

您可以在Person类中实现类似于构建器模式的内容.该方法涉及$this在每个setter调用结束时返回.

$person
    ->set_first_name('John')
    ->set_last_name('Smith')
    ->set_email('spam_me@this-place.com');
Run Code Online (Sandbox Code Playgroud)

在你的班上......

class Person {
    private $first_name;
    ...
    public function set_first_name($first_name) {
        $this->first_name = $first_name;
        return $this;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)