[PHP] Re: PHP5 Static functions called through __call() that don't exist... yet
Hi, I also think this a good proposal, but you might consider writing to the internals group, as this group is only for discussions and most of the people that read it are PHP users and have no idea how the language is made (including me). Regards, Emil Ivanov Jared Farrish [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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(divis_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){ 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-generalm=114558851102060w=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. $$ -- Jared Farrish Intermediate Web Developer Denton, Tx Abraham Maslow: If the only tool you have is
[PHP] Re: PHP5 Static functions called through __call() that don't exist... yet
Jared Farrish wrote: 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){ 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-generalm=114558851102060w=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? Hi Jared, I think you meant to post to php-general, but I can answer your question. ***PLEASE DO NOT REPLY TO PEAR-GENERAL THANK YOU*** You can achieve what you want through this kind of code ?php class TypeChecker { private static $types = array( 'array','bool','float','integer','null','numeric', 'object','resource','scalar','string' ); public static $singleton; static function init() { self::$singleton = new TypeChecker; } function __call($method, $args) { $test = new StdClass; foreach (self::$types as $thing) { $test-$thing = ${is$thing}($args[0]); } return $test-$method; } } TypeChecker::init() echo TypeChecker::$singleton-string('test'); ? However, I don't see any benefit to using static methods here. Just use an object. ?php class TypeChecker { private $types = array( 'array','bool','float','integer','null','numeric', 'object','resource','scalar','string' ); function __call($method, $args) { $test = new StdClass; foreach (self::$types as $thing) { $test-$thing = ${is$thing}($args[0]); } return $test-$method; } } $check = new TypeChecker; echo $check-string('test'); ? If you're trying to do several assertions and separate them into classes, do something like so: ?php class Tester { public $type; ... function __construct() { $this-type = new TypeChecker; } } $check = new Tester; echo $check-type-string('test'); ? Greg -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: PHP5 Static functions called through __call() that don't exist... yet
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(divis_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){ 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-generalm=114558851102060w=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. $$ -- 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. $$