Re: PHP5 Static functions called through __call() that don't exist... yet
| From: | Jared Farrish | Date: | Sat, 19 May 2007 03:00:08 +0000 |
| Subject: | Re: PHP5 Static functions called through __call() that don't exist... yet | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-255227@lists.php.net to get a copy of this message | ||
Hi all,
Here is more code, with a test case included. What I would prefer to do is
call TypeAssist::$string(), instead of TypeAssist::$a->string(). Or at least
__construct() the $a object.
<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>
Original Message Text
Hi all, I am building an assertType object using static functions. What I want to keep away from is the following: <code> public static function assertString($para){-- 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." $$return $answer;}; public static function assertBool($para){return $answer;}; ... public static function assertArray($para){return $answer;}; </code> What I would like to do is replace this with the following: <code> if (!class_exists('TypeAssert')) {class TypeAssert { private static $types = array( 'array','bool','float','integer','null','numeric', 'object','resource','scalar','string' ); public static function __call($method,$arguments) { $obj = self::assertStandardTypes($arguments[0]); return $obj->$method; } public static function assertStandardTypes($para) { $r = TypeAssert::getTypesObject(); if (is_array($para)) $r->array = true; if (is_bool($para)) $r->bool = true; if (is_float($para)) $r->float = true; if (is_integer($para)) $r->integer = true; if (is_null($para)) $r->null = true; if (is_numeric($para)) $r->numeric = true; if (is_object($para)) $r->object = true; if (is_resource($para)) $r->resource = true; if (is_scalar($para)) $r->scalar = true; if (is_string($para)) $r->string = true; return $r; } public static function getTypesObject() { $obj = (object) ''; for ($i = 0; $i < count(self::$types); $i++) { $obj->{self::$types[$i]} = (bool) false; } return $obj; } }} echo('<pre>'); echo(TypeAssert::string('test')); echo('</pre>'); </code> I don't think this is possible (see http://marc.info/?l=php-general&m=114558851102060&w=2 ). But I would LIKE for it to work (currently, the above code doesn't). Anybody have any insight on how I might get this to work? 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." $$