Define a setter and getter : Class Property « Class « PHP






Define a setter and getter

 
<?php
   class Employee{
      private $employeeid;

      function setEmployeeID($employeeid) {
         $this->employeeid = $employeeid;
      }
      function getEmployeeID() {
         return $this->employeeid;
      }
   }
   $drone1 = new Employee();

   $drone1->setEmployeeID("12345");
   $drone2 = clone $drone1;
   $drone2->setEmployeeID("67890");

   echo "drone1 employeeID: ".$drone1->getEmployeeID()."<br />";
   echo "drone2 employeeID: ".$drone2->getEmployeeID()."<br />";

   $drone1 = new Employee();

   $drone1->setEmployeeID("12345");

   $drone2 = clone $drone1;

   $drone2->setEmployeeID("67890");

   echo "employeeID: ".$drone1->getEmployeeID()."<br />";
   echo "employeeID: ".$drone2->getEmployeeID()."<br />";
?>
  
  








Related examples in the same category

1.Assign default property value
2.Changing the Value of a Property from Within a Method
3.Define boolean Class properties
4.Accessing a Property from Within a Method
5.Adding Properties
6.Adding the $age variable to Cat
7.Changing the Value of a Property from Within a Method
8.Class including a complete collection of get and set methods.
9.Controlling Access to Class Members