·  PHP – Syntax – OOP – Classes – Methods – Predefined – _get

 

- Info:

  - _get() method takes as input name of the property whose value you’d like to retrieve.

  - Returns TRUE if the method is successfully executed, and FALSE otherwise.

  - You can overload this method with custom implementation.

 

- Example:

  - This example shows how to simulate getters and setters through private field and publich methods.

  - Creat PHP Console Application Test.php.

 

    Test.php

    <?php

   

      class Employee {

        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 Employee();

      $employee->name = "Mario";

      echo $employee->name."<br />";

      echo $employee->age;

      

    ?>