use yii\base\Model; class MyModel extends Model { public $name; public $email; public function init() { $this->name = 'John Doe'; $this->email = 'johndoe@example.com'; parent::init(); } } $model = new MyModel(); echo $model->name; // Output: John Doe echo $model->email; // Output: johndoe@example.com
use yii\base\Model; class MyModel extends Model { public $name; public $email; public function init() { $this->name = 'John Doe'; $this->email = $this->generateEmail(); parent::init(); } private function generateEmail() { return strtolower(str_replace(' ', '.', $this->name)) . '@example.com'; } } $model = new MyModel(); echo $model->name; // Output: John Doe echo $model->email; // Output: john.doe@example.comIn this example, we use the init() method to initialize the name property with a fixed value and the email property with a dynamically generated value based on the name property. We create a private method generateEmail() to generate the email address. Then we create an instance of the model and output its properties. Package library: The yii\base\Model class is part of the Yii framework, an open-source PHP framework for web development.