Re: Q on Primitives

From: Date: Thu, 18 Dec 2008 22:22:05 +0000
Subject: Re: Q on Primitives
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-42339@lists.php.net to get a copy of this message
Dave Ingram wrote:
I remember that multiple signatures was said to have a possible very difficult implementation. However, a similar behaviour can be achieved by some instanceof().
    
I thought it probably would be awkward, but we do already have some type hinting that can also be accomplished with instanceof() -- this is just an extension of that idea. Although it's not my idea as such! instance of is handy however it isn't the cleanest; consider:
public static function parseByte( $var ) {
       $testPassed = false;
       if( $var instanceof Number ) {
           $var = $var->byteValue();
           $testPassed = true;
       } if( $var instanceof String ) {
           $var = $var->__toString();
       }
       if( !$testPassed && is_numeric( $var ) ) {
           $var = 0 + $var;
           $testPassed = true;
       }
       if( $testPassed && Byte::MIN_VALUE <= $var && Byte::MAX_VALUE >= $var ) {
           return $var;
       }
       throw new NumberFormatException();
} could be: public static function parseByte( Number $var ) {
       return $var->byteValue();
} public static function parseByte( String $var ) {
       if( is_numeric( $var = $var->__toString() ) ) {
         $var = 0 + $var;
         if( Byte::MIN_VALUE <= $var && Byte::MAX_VALUE >= $var ) {
           return $var;
         }
       }
       throw new NumberFormatException();
} mini-use-case but shows how useful it would be..

« previous php.internals (#42339) next »