For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > May 2007 > Re: [PEAR] PHP5 Static functions called through __call() that don't exist... yet









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PEAR] PHP5 Static functions called through __call() that don't exist... yet
Jared Farrish

2007-05-22, 7:02 pm

Thank everyone for their suggestion. I would like to see a __static()
version of __call(), but this is the wrong place to bring that feature
request up.

To answer Greg Beaver's observations, I would prefer to use static instances
in this case, to save myself the trouble (and overhead) of instantiating a
new object while developing classes. The utility is meant to provide unit
testing for individual classes or libraries, that can then be extended to a
specific class or library, and abstract the actually is_type() testing to
another class. It's a somewhat specific implementation meant more for unit
testing.

Below is the code I decided to implement (with a test below it to
demonstrate):

<code>
<?php
if (!class_exists('TypeAssert')) {
class TypeAssert {
public static $a;
public static $assert;
private static $types = array(
'array','bool','float','integer','null',
'numeric',
'object','resource','scalar','string'
);
function __construct() {
self::$assert =& self::$a;
}
public static function __call($method,$arguments) {
$obj = self::assertStandardTypes($arguments[0])
;
return $obj->$method;
}
public static function assertStandardTypes($para) {
$r = TypeAssert::getTypesObject();
foreach ($r as $type=>$v) {
$func = "is_".strtolower($type);
if (function_exists($func) === true) {
if ($func($para) === true) {
$r->$type = true;
} else {
$r->$type = false;
}
}
}
return $r;
}
public static function getTypesObject() {
$obj = (object) '';
for ($i = 0; $i < count(self::$types); $i++) {
$obj->{self::$types[$i]} = (bool) false;
}
return $obj;
}
}
}
TypeAssert::$a = new TypeAssert();
echo("<pre>\n");
switch($_GET['type']) {
case 'int':
$test = 100;
$_test = 100;
break;
case 'float':
$test = 100.001;
$_test = 100.001;
break;
case 'null':
$test = null;
$_test = 'null';
break;
case 'object':
$test = TypeAssert::$a;
$_test = '[object]';
break;
default:
$test = 'string';
$_test = 'string';
break;
}
foreach (TypeAssert::getTypesObject() as $type => $v) {
echo("<div>is_<b style=\"color: #00a;\">$type</b>(<b>$_test</b> ) === ".
(TypeAssert::$assert->$type($test)?
'<b style="color: #0a0;">true</b>':
'<b style="color: #a00;">false</b>').
"</div>\n"
);
}
echo("</pre>\n");
?>
</code>

Thanks!

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com