Define and call getter method : Getter Setter : Class PHP Source Code


PHP Source Code » Class » Getter Setter »

 

Define and call getter method




<?php
   class Staff
   {
      var $name;
      var $city;
      protected $wage;

      function __get($propName)
      {
         echo "__get called!<br />";
         $vars = array("name","city");
         if (in_array($propName, $vars))
         {
            return $this->$propName;
         else {
            return "No such variable!";
         }
      }
   }

   $employee = new Staff();
   $employee->name = "Joe";
   echo $employee->name."<br/>";
   echo $employee->age;
?>
           
       



HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Class
» Getter Setter