Re: PHP5 Static functions called through __call() that don't exist... yet

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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-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?

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux