AW: [PEAR] PHP5 Static functions called through __call() that don't exist... yet
| From: | xmurrix | Date: | Sat, 19 May 2007 21:25:38 +0000 |
| Subject: | AW: [PEAR] PHP5 Static functions called through __call() that don't exist... yet | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-27333@lists.php.net to get a copy of this message | ||
Hi Jared,
this won't work, if you use a static class. I'm not shure, but in my opinion
all magic methods are usable in classes which can be instanciated.
Following snipped should work:
[snip]
if (!class_exists('TypeAssert')) {
class TypeAssert {
private static $types = array(
'array','bool','float','integer','null','numeric',
'object','resource','scalar','string'
);
public function __call($method,$arguments) {
$obj = $this->assertStandardTypes($arguments[0]);
return $obj->$method;
}
public function assertStandardTypes($para) {
$r = $this->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 function getTypesObject() {
$obj = (object) '';
for ($i = 0; $i < count(self::$types); $i++) {
$obj->{self::$types[$i]} = (bool) false;
}
return $obj;
}
}
}
$TypeAssert = new TypeAssert();
echo('<pre>');
echo($TypeAssert->string('test'));
echo('</pre>');
[/snap]
Greetings
Murat Purc
> -----Ursprüngliche Nachricht-----
> Von: Jared Farrish [mailto:farrishj@gmail.com]
> Gesendet: Freitag, 18. Mai 2007 17:20
> An: pear-general@lists.php.net
> Betreff: [PEAR] PHP5 Static functions called through __call() that don't
> exist... yet
>
> 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-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." $$