PHP/Class/ call

Материал из Web эксперт
Версия от 10:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

__call( ) method is called if PHP fails to find the method

   <source lang="html4strict">

<?

   class Dog {
           public $Name;
           public function bark( ) {
                   print "Woof!\n";
           }
           public function __call($function, $args) {
                   $args = implode(", ", $args);
                   print "Call to $function( ) with args "$args" failed!\n";
           }
   }
   $poppy = new Dog;
   $poppy->meow("foo", "bar", "baz");

?>

 </source>
   
  


Intercepting Method Calls with the __call() Method (PHP 5 Only)

   <source lang="html4strict">

<?php function r( $item_array, $immediately=false ) {

 return "Registering
\n";

} class Item {

 public $name = "item";
 public $price = 0;
 function __call( $method, $args ) {
   $bloggsfuncs = array ( "r");
   if ( in_array( $method, $bloggsfuncs ) ) {
     array_unshift( $args, get_object_vars( $this ) );
     return call_user_func( $method, $args );
   }
 }

} $item = new Item(); print $item->r( true ); ?>

 </source>
   
  


Using the __call() Method

   <source lang="html4strict">

<?php

    class ParentClass {
         function __call($method, $params) {
              echo "The method $method doesn"t exist!\n";
         }
    }
    class ChildClass extends ParentClass {
         function myFunction() {
         }
    }
    $inst = new ChildClass();
    $inst->nonExistentFunction();

?>

 </source>