PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » PHP » PHP_Compat » Bug #8208

is_callable missing in php 4.0.4, 4.0.5

Details

Request #8208is_callable missing in php 4.0.4, 4.0.5
Submitted2006-07-13 14:19 UTC
Fromgiunta dot gaetano at sea-aeroportimilano dot it
Assignedarpad
StatusClosed
PackagePHP_Compat
PHP Version4.4.2
OSirrelevant
Roadmaps1.6.0a1

Comments

[2006-07-13 14:19 UTC] giunta dot gaetano at sea-aeroportimilano dot it

Description:
------------
Maybe not the most important function in the world, but I found a very old install of PHP 4.0.4 hanging around (in production!), so I whipped up a replacement. Here it is, complete w. unit test

<?php
/**
* Replace function is_callable()
*
* @category PHP
* @package PHP_Compat
* @link http://php.net/function.is_callable
* @author Gaetano Giunta <giunta.gaetano@sea-aeroportimilano.it>
* @version $Revision: 0.1 $
* @since PHP 4.0.6
* @require PHP 4.0.0 (true, false, etc...)
* @todo add the 3rd parameter syntax...
*/
if (!function_exists('is_callable')) {
function is_callable($var, $syntax_only=false)
{
if ($syntax_olny)
{
/* from The Manual:
* If the syntax_only argument is TRUE the function only verifies
* that var might be a function or method. It will only reject simple
* variables that are not strings, or an array that does not have a
* valid structure to be used as a callback. The valid ones are
* supposed to have only 2 entries, the first of which is an object
* or a string, and the second a string
*/
return (is_string($var) || (is_array($var) && count($var) == 2 && is_string(end($var)) && (is_string(reset($var)) || is_object(reset($var)))));
}
else
{
if (is_string($var))
{
return function_exists($var);
}
else if (is_array($var) && count($var) == 2 && is_string($method = end($var)))
{
$obj = reset($var);
if (is_string($obj))
{
$methods = get_class_methods($obj);
return (bool)(is_array($methods) && in_array(strtolower($method), $methods));
}
else if (is_object($obj))
{
return method_exists($obj, $method);
}
}
return false;
}
}
}

?>

Test script:
---------------
include('is_callable.php');
include('xmlrpc.inc');
$a = new xmlrpcval('hello world');

var_dump(is_callable(1));
var_dump(is_callable(false));
var_dump(is_callable(array('xmlrpcval', 'serialize', 'me')));
var_dump(is_callable(array('xmlrpcvaz', 'serialize')));
var_dump(is_callable(array('xmlrpcval', 'serializez')));
var_dump(is_callable(array($a, 'serializez')));
var_dump(is_callable(array($a, 'serialize', 'me')));

var_dump(is_callable('is_callable'));
var_dump(is_callable('is_cAllable'));
var_dump(is_callable(array('xmlrpcval', 'serialize')));
var_dump(is_callable(array('Xmlrpcval', 'serialize')));
var_dump(is_callable(array('Xmlrpcval', 'Serialize')));
var_dump(is_callable(array($a, 'serialize')));
var_dump(is_callable(array($a, 'Serialize')));

Expected result:
----------------
(tested against php 4.4...)

bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)