PHP/Class/Properties set

Материал из Web эксперт
Перейти к: навигация, поиск

set property

<?php
   class Staff
   {
      var $name;
      function __set($propName, $propValue)
      {
         $this->$propName = $propValue;
      }
   }
   $employee = new Staff();
   $employee->name = "Joe";
   $employee->title = "Programmer";
   
   echo "Name: ".$employee->name;
   echo "<br />";
   echo "Title: ".$employee->title;
?>



Use poroperty set

<?php
   class Staff
   {
      var $name;
      function __set($propName, $propValue)
      {
         echo "Nonexistent variable: \$$propName!";
      }
   }
   $employee = new Staff();
   $employee->name = "Joe";
   $employee->title = "Developer";
?>