For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > May 2007 > AW: [PEAR] PHP5 Static functions called through __call() that don't exist... yet









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2007-05-19, 7:01 pm

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 =3D array(
'array','bool','float','integer','null',
'numeric',
'object','resource','scalar','string'
);
public function __call($method,$arguments) {
$obj =3D $this->assertStandardTypes($arguments[0]);
return $obj->$method;
}
public function assertStandardTypes($para) {
$r =3D $this->getTypesObject();
if (is_array($para)) $r->array =3D true;
if (is_bool($para)) $r->bool =3D true;
if (is_float($para)) $r->float =3D true;
if (is_integer($para)) $r->integer =3D true;
if (is_null($para)) $r->null =3D true;
if (is_numeric($para)) $r->numeric =3D true;
if (is_object($para)) $r->object =3D true;
if (is_resource($para)) $r->resource =3D true;
if (is_scalar($para)) $r->scalar =3D true;
if (is_string($para)) $r->string =3D true;
return $r;
}
public function getTypesObject() {
$obj =3D (object) '';
for ($i =3D 0; $i < count(self::$types); $i++) {
$obj->{self::$types[$i]} =3D (bool) false;
}
return $obj;
}
}
}
$TypeAssert =3D new TypeAssert();
echo('<pre>');
echo($TypeAssert->string('test'));
echo('</pre>');
[/snap]


Greetings

Murat Purc


> -----Urspr=FCngliche 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
>=20
> Hi all,
>=20
> I am building an assertType object using static functions. What I want =

to
> keep away from is the following:
>=20
> <code>
> public static function assertString($para){
> return $answer;
> };
> public static function assertBool($para){
> return $answer;
> };
> ...
> public static function assertArray($para){
> return $answer;
> };
> </code>
>=20
> What I would like to do is replace this with the following:
>=20
> <code>
> if (!class_exists('TypeAssert')) {
> class TypeAssert {
> private static $types =3D array(
> 'array','bool','float','integer','null',
'numeric',
> 'object','resource','scalar','string'
> );
> public static function __call($method,$arguments) {
> $obj =3D self::assertStandardTypes($arguments[0])
;
> return $obj->$method;
> }
> public static function assertStandardTypes($para) {
> $r =3D TypeAssert::getTypesObject();
> if (is_array($para)) $r->array =3D true;
> if (is_bool($para)) $r->bool =3D true;
> if (is_float($para)) $r->float =3D true;
> if (is_integer($para)) $r->integer =3D true;
> if (is_null($para)) $r->null =3D true;
> if (is_numeric($para)) $r->numeric =3D true;
> if (is_object($para)) $r->object =3D true;
> if (is_resource($para)) $r->resource =3D true;
> if (is_scalar($para)) $r->scalar =3D true;
> if (is_string($para)) $r->string =3D true;
> return $r;
> }
> public static function getTypesObject() {
> $obj =3D (object) '';
> for ($i =3D 0; $i < count(self::$types); $i++) {
> $obj->{self::$types[$i]} =3D (bool) false;
> }
> return $obj;
> }
> }
> }
> echo('<pre>');
> echo(TypeAssert::string('test'));
> echo('</pre>');
> </code>
>=20
> I don't think this is possible (see
> http://marc.info/?l=3Dphp-general&m...851102060&w=3D2). But I =

would LIKE
> for
> it to work (currently, the above code doesn't).
>=20
> Anybody have any insight on how I might get this to work?
>=20
> Thanks!
>=20
> --
> Jared Farrish
> Intermediate Web Developer
> Denton, Tx
>=20
> Abraham Maslow: "If the only tool you have is a hammer, you tend to =

see
> every problem as a nail." $$

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com