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

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

 



Ok, somehow I did this again (posted to pear-general instead of
php-general). pear-general and php-general look alike...

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!

On 5/22/07, Jared Farrish <farrishj@xxxxxxxxx> wrote:

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." $$




--
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." $$

[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