  3



  
          __call, __get  __set.        ,        ,    .  :

void __set ( string , mixed  )

void __get ( mixed  )

               ,   .      ,    .    __set()   ,        .

    __get  __set:

<?php
class Setter {
   public $n;
   private $x = array("a" => 1, "b" => 2, "c" => 3);

   function __get($nm) {
     print " [$nm]\n";

     if (isset($this->x[$nm])) {
       $r = $this->x[$nm];
       print ": $r\n";
       return $r;
     } else {
       print "!\n";
     }
   }

   function __set($nm, $val) {
     print " $val  [$nm]\n";

     if (isset($this->x[$nm])) {
       $this->x[$nm] = $val;
       print "OK!\n";
     } else {
       print " !\n";
     }
   }
}

$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump($foo);
?>
    :

 100  [a]
OK!
 [a]
: 100
 101  [a]
OK!
 [z]
!
 1  [z]
 !
object(Setter)#1 (2) {
   ["n"]=>
   int(1)
   ["x:private"]=>
   array(3) {
     ["a"]=>
     int(101)
     ["b"]=>
     int(2)
     ["c"]=>
     int(3)
   }
}




 
        __call, __get  __set.        ,        ,    . :

mixed __call ( string , array  )

   ,          ,   .       . ,      ,    . ,   __call(),    .


    __call:

<?php
class Caller {
   private $x = array(1, 2, 3);

   function __call($m, $a) {
     print "  $m :\n";
     var_dump($a);
     return $this->x;
   }
}

$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);
?>
   :

  test:
array(4) {
   [0]=>
   int(1)
   [1]=>
   string(1) "2"
   [2]=>
   float(3.4)
   [3]=>
   bool(true)
}
array(3) {
   [0]=>
   int(1)
   [1]=>
   int(2)
   [2]=>
   int(3)
}






     ,  ,       ,     .

   ,    ,      "interface";      .           "implements"    ,    .   ,           .

   -         ,         , ,      .  :

<?php
interface ITemplate
{
   public function setVariable($name, $var);
   public function getHtml($template);
}

class Template implements ITemplate
{
   private $vars = array();
   
   public function setVariable($name, $var)
   {
     $this->vars[$name] = $var;
   }
   
   public function getHtml($template)
   {
     foreach($this->vars as $name => $value) {
       $template = str_replace('{'.$name.'}', $value, $template);
     }
     
     return $template;
   }
}
?>




 instanceof
     .  is_a(),   PHP 4,    .

<?php 
if ($obj instance of Circle) { 
     print '$obj is a Circle'; 
} 
?>




 final
  final    ,       .          final,        , :

<?php
class BaseClass {
    public function test() {
        echo "  BaseClass::test()\n";
    }
    
    final public function moreTesting() {
        echo "  BaseClass::moreTesting()\n";
    }
}

class ChildClass extends BaseClass {
    public function moreTesting() {
        echo "  ChildClass::moreTesting()\n";
    }
}
//    : 
//Cannot override final method BaseClass::moreTesting()
// ( BaseClass::moretesting()    )
?>



,   final
   final     .    :

<?php 
final class FinalClass { 
} 

class BogusClass extends FinalClass { 
} 
?>


