PHP/Class/ call — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
__call( ) method is called if PHP fails to find the method
<?
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");
?>
Intercepting Method Calls with the __call() Method (PHP 5 Only)
<?php
function r( $item_array, $immediately=false ) {
return "Registering<br />\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 );
?>
Using the __call() Method
<?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();
?>