class User extends \yii\base\Model { public $name; public $email; public $phone; public function __get($name) { if (isset($this->$name)) { return $this->$name; } return null; } } $user = new User(); $user->name = 'John'; $user->email = 'john@example.com'; $user->phone = '123-456-7890'; echo $user->name; // Output: John
class User extends \yii\base\Model { public $firstName; public $lastName; public function __get($name) { if ($name == 'fullName') { return $this->firstName . ' ' . $this->lastName; } return parent::__get($name); } } $user = new User(); $user->firstName = 'John'; $user->lastName = 'Doe'; echo $user->fullName; // Output: John DoeIn this example, the __get method is used to implement a virtual property called fullName, which is a combination of the firstName and lastName properties. Package Library: yii2-base