pbx_callbacks -- a custom class in pbx_callbacks.php
本页面仅提供英文版本。
<?php
/**
* PHP By Example
*
* @copyright 2014 Michel Corne
* @license http://www.opensource.org/licenses/gpl-3.0.html GNU GPL v3
*/
/*
* Custom functions and methods used as callbacks in examples
*
* a callback may be done in 4 ways:
*
* a call to a function : $result = call_user_func("barber", "shave");
*
* a call to a closure : $result = call_user_func($barber, "shave");
*
* a call to a static method : $result = call_user_func(["pbx_callbacks", "barber"], "shave");
* $result = call_user_func(["pbx_callbacks::barber"] , "shave");
*
* a call to an objet method : $object = new pbx_callbacks();
* $result = call_user_func([$object, "barber"], "shave");
*
* Note that all functions and closures must have a corresponding method so they can be elligible to run,
* see filter::is_custom_callback_function().
*/
/**
* List of all custom functions used as callbacks
*/
function barber($type)
{
return "You wanted a $type haircut, no problem";
}
function cb1($a)
{
return array ($a);
}
function cb2($a, $b)
{
return array ($a, $b);
}
function compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1 : -1;
}
function cube($n)
{
return($n * $n * $n);
}
function double($value)
{
return $value * 2;
}
function even($var)
{
return(!($var & 1));
}
function foo($value)
{
// Expected format: Surname, GivenNames
if (strpos($value, ", ") === false)
return false;
$GLOBALS['to_lower'] = function ($matches)
{
return strtolower($matches[0]);
};
/**
* List of all methods used as callbacks
*
* This class is used for unit testing.
*/
class pbx_callbacks
{
/**
* Calls the corresponding static method when the callback is called from an instance of this class
*
* this is the prefered way vs calling the function directly in order to report a call to an invalid callback as an "invalid" method
*
* @param string $name
* @param array $arguments
* @return mixed
*/
function __call($name, $arguments)
{
return call_user_func_array(self::$name, $arguments);
}
/**
* List of all static methods used for callbacks
*/
static function barber($type)
{
return "You wanted a $type haircut, no problem";
}
static function cb1($a)
{
return array ($a);
}
static function cb2($a, $b)
{
return array ($a, $b);
}