⊗ppOpBsPS 13 of 107 menu

Property Setters in OOP in PHP

For writing private properties, they also create methods called setters. Their names usually start with the word set. Let's create setters for the properties:

<?php class User { private $name; private $surn; public function setName($name) { $this->name = $name; } public function setSurn($surn) { $this->surn = $surn; } public function getName() { return $this->name; } public function getSurn() { return $this->surn; } } ?>

Let's test the work of getters and setters. Let's create an object of our class:

<?php $user = new User(); ?>

Using setters, set the property values:

<?php $user->setName('john'); $user->setSurn('smit'); ?>

Using getters, output the property values:

<?php echo $user->getName(); echo $user->getSurn(); ?>

Add property setters to your Employee class.

bydeenesfrptru