⊗ppOpBsCPP 9 of 107 menu

Properties Through Constructor Parameters in OOP in PHP

Variables passed through constructor parameters can be written to object properties:

<?php class User { public $name; public $surn; public function __construct($name, $surn) { $this->name = $name; $this->surn = $surn; } } ?>

Thus, the passed values will become available in all methods of the class. As an example, let's use the passed values in some method:

<?php class User { public $name; public $surn; public function __construct($name, $surn) { $this->name = $name; $this->surn = $surn; } public function show() { return $this->name . ' ' . $this->surn; } } ?>

Let's check how this works. Let's create a new object, passing the user's name and surname as parameters to it:

<?php $user = new User('john', 'smit'); ?>

Now let's call our method:

<?php echo $user->show(); ?>

Pass to the constructor of the class Employee the name and salary of the employee and write them to the corresponding properties.

Create a method that will display the employee's name.

Create a method that will display the employee's salary.

Create a method that will increase the employee's salary by 10%.

byenru