타입 힌트

PHP 5 에서 타입 힌트를 도입했습니다. 함수는 이제 파라미터를 객체(함수 프로토 타입에 클래스의 이름을 명시하는것으로), 인터페이스, 배열(PHP 5.1 이후) 또는 callable(PHP 5.4 이후) 로 강제할수 있게 되었습니다. 하지만, NULL 을 디폴트 값으로 지정하게 되면, 이후 호출시 NULL을 인자로 넘기는것도 가능합니다.

클래스나 인터페이스가 타입힌트로 사용되면 그의 모든 자식이나 구현체들은 인자로 넘기는게 가능합니다.

타입 힌트는 int 또는 string 과 같은 스칼라 타입은 사용될수 없습니다. 리소스트레이트 또한 사용될 수 없습니다.

Example #1 타입 힌트 예제

<?php
// An example class
class MyClass
{
    
/**
     * A test function
     *
     * 첫번째 파라미터는 OtherClass 타입의 객체여야 함.
     */
    
public function test(OtherClass $otherclass) {
        echo 
$otherclass->var;
    }


    
/**
     * Another test function
     *
     * 첫번재 파라미터는 배열 이어야 함.
     */
    
public function test_array(array $input_array) {
        
print_r($input_array);
    }
    
    
/**
     * 첫번째 파라미터는 iterator 이어야 함.
     */
    
public function test_interface(Traversable $iterator) {
        echo 
get_class($iterator);
    }
    
    
/**
     * 첫번째 파라미터는 callable 이어야 함.
     */
    
public function test_callable(callable $callback$data) {
        
call_user_func($callback$data);
    }
}

// Another example class
class OtherClass {
    public 
$var 'Hello World';
}
?>

타입 힌트를 충족하지 않을시 fatal error 결과.

<?php
// 각 클래스의 인스턴스
$myclass = new MyClass;
$otherclass = new OtherClass;

// Fatal Error: Argument 1 must be an object of class OtherClass
$myclass->test('hello');

// Fatal Error: Argument 1 must be an instance of OtherClass
$foo = new stdClass;
$myclass->test($foo);

// Fatal Error: Argument 1 must not be null
$myclass->test(null);

// Works: Prints Hello World
$myclass->test($otherclass);

// Fatal Error: Argument 1 must be an array
$myclass->test_array('a string');

// Works: Prints the array
$myclass->test_array(array('a''b''c'));

// Works: Prints ArrayObject
$myclass->test_interface(new ArrayObject(array()));

// Works: Prints int(1)
$myclass->test_callable('var_dump'1);
?>

타입힌트는 함수에서도 동작합니다:

<?php
// An example class
class MyClass {
    public 
$var 'Hello World';
}

/**
 * A test function
 *
 * First parameter must be an object of type MyClass
 */
function myFunction(MyClass $foo) {
    echo 
$foo->var;
}

// Works
$myclass = new MyClass;
myFunction($myclass);
?>

타입힌트에 NULL을 지정할 수 있습니다:

<?php

/* NULL 값 허용 */
function test(stdClass $obj NULL) {

}

test(NULL);
test(new stdClass);

?>
add a note add a note

User Contributed Notes 23 notes

up
73
Daniel dot L dot Wood at Gmail dot Com
16 years ago
People often ask about scalar/basic typehints.  Here is a drop in class that I use in my MVC framework that will enable typehints through the use of a custom error handler.

Note: You should include this code above all other code in your include headers and if you are the using set_error_handler() function you should be aware that this uses it as well.  You may need to chain your set_error_handlers()

Why?
1) Because people are sick of using the is_* functions to validate parameters.
2) Reduction of redundant coding for defensive coders.
3) Functions/Methods are self defining/documenting as to required input.

Also..
Follow the discussion for typehints in PHP 6.0 on the PHP Internals boards.

<?php

define
('TYPEHINT_PCRE'              ,'/^Argument (\d)+ passed to (?:(\w+)::)?(\w+)\(\) must be an instance of (\w+), (\w+) given/');

class
Typehint
{

    private static
$Typehints = array(
       
'boolean'   => 'is_bool',
       
'integer'   => 'is_int',
       
'float'     => 'is_float',
       
'string'    => 'is_string',
       
'resrouce'  => 'is_resource'
   
);

    private function
__Constrct() {}

    public static function
initializeHandler()
    {

       
set_error_handler('Typehint::handleTypehint');

        return
TRUE;
    }

    private static function
getTypehintedArgument($ThBackTrace, $ThFunction, $ThArgIndex, &$ThArgValue)
    {

        foreach (
$ThBackTrace as $ThTrace)
        {

           
// Match the function; Note we could do more defensive error checking.
           
if (isset($ThTrace['function']) && $ThTrace['function'] == $ThFunction)
            {

               
$ThArgValue = $ThTrace['args'][$ThArgIndex - 1];

                return
TRUE;
            }
        }

        return
FALSE;
    }

    public static function
handleTypehint($ErrLevel, $ErrMessage)
    {

        if (
$ErrLevel == E_RECOVERABLE_ERROR)
        {

            if (
preg_match(TYPEHINT_PCRE, $ErrMessage, $ErrMatches))
            {

                list(
$ErrMatch, $ThArgIndex, $ThClass, $ThFunction, $ThHint, $ThType) = $ErrMatches;

                if (isset(
self::$Typehints[$ThHint]))
                {

                   
$ThBacktrace = debug_backtrace();
                   
$ThArgValue  = NULL;

                    if (
self::getTypehintedArgument($ThBacktrace, $ThFunction, $ThArgIndex, $ThArgValue))
                    {

                        if (
call_user_func(self::$Typehints[$ThHint], $ThArgValue))
                        {

                            return
TRUE;
                        }
                    }
                }
            }
        }

        return
FALSE;
    }
}

Typehint::initializeHandler();

?>

An are some examples of the class in use:

<?php

function teststring(string $string) { echo $string; }
function
testinteger(integer $integer) { echo $integer; }
function
testfloat(float $float) { echo $float; }

// This will work for class methods as well.

?>

You get the picture..
up
28
michaelrfairhurst at gmail dot com
11 years ago
The scalar type hinting solutions are all overthinking it. I provided the optimized regex version, as well as the fastest implementation I've come up with, which just uses strpos. Then I benchmark both against the TypeHint class.

<?php
function optimized_strpos($ErrLevel, $ErrMessage) {
        if (
$ErrLevel == E_RECOVERABLE_ERROR
           
// order this according to what your app uses most
           
return strpos($ErrMessage, 'must be an instance of string, string')
                ||
strpos($ErrMessage, 'must be an instance of integer, integer')
                ||
strpos($ErrMessage, 'must be an instance of float, double')
                ||
strpos($ErrMessage, 'must be an instance of boolean, boolean')
                ||
strpos($ErrMessage, 'must be an instance of resource, resource');
}

function
optimized_regex($ErrLevel, $ErrMessage) {
        if (
$ErrLevel == E_RECOVERABLE_ERROR) {
            if(
preg_match('/^Argument \d+ passed to (?:\w+::)?\w+\(\) must be an instance of (\w+), (\w+) given/', $ErrMessage, $matches))
                return
$matches[1] == ($matches[2] == 'double' ? 'float' : $matches[2]);
        }
}
?>

BENCHMARKING Typehint::handleTypehint()
string type hint.....[function it(string $var) {}]............2.1588530540466 seconds
float type hint......[function it(float $var) {}].............2.1563150882721 seconds
integer type hint....[function it(integer $var) {}]...........2.1579530239105 seconds
boolean type hint....[function it(boolean $var) {}]...........2.1590459346771 seconds

BENCHMARKING optimized_regex()
string type hint.....[function it(string $var) {}]............0.88872504234314 seconds
float type hint......[function it(float $var) {}].............0.88528990745544 seconds
integer type hint....[function it(integer $var) {}]...........0.89038777351379 seconds
boolean type hint....[function it(boolean $var) {}]...........0.89061188697815 seconds

BENCHMARKING optimized_strpos()
string type hint.....[function it(string $var) {}]............0.52635812759399 seconds
float type hint......[function it(float $var) {}].............0.74228310585022 seconds
integer type hint....[function it(integer $var) {}]...........0.63721108436584 seconds
boolean type hint....[function it(boolean $var) {}]...........0.8429491519928 seconds
up
8
kalkamar at web dot de
16 years ago
I really like the Daniel`s Typehinting-Class, but you please not that it may be relevant for the performance if you use Typehinting for scalar values very often.

Here is my performance-test:

<?php

function notypehinting($x)
{
   
is_string($x);   //checking the type manually instead
}

function
typehinting(string $x)
{
}

$test=new timer;
for(
$i=0;$i<10000;$i++)
{
try{
   
notypehinting('test');
}
catch(
Exception $e){}
}
echo
$test.'<br>';

$test2=new timer;
for(
$i=0;$i<10000;$i++)
{
try{
   
typehinting('test');
}
catch(
Exception $e){}
}
echo
$test2.'<br>';
?>

Output:

0.0088460445404053
0.21634602546692

Result:
typehinting() ist more than 20 times slower than notypehinting()

You see: typehinting for scalar types (like suggested by Daniel) is not the best thing for the performance if you use it very often.
up
5
MiChAeLoKGB
7 years ago
Please note, that with PHP 7, type hinting is faster than normal implementation.
This has to do with type hinting before PHP 7 allowing only objects and arrays and anything else (scalars) would throw an error.

I did some using code "doom at doom dot pl" posted 2 years ago

Here are results from couple of test runs using PHP 7 (200.000 loops)
- teststringNormal took: 0.01799488067627
- teststringOverhead took: 0.012195825576782

- teststringNormal took: 0.027030944824219
- teststringOverhead took: 0.012197017669678

- teststringNormal took: 0.017856121063232
- teststringOverhead took: 0.012274980545044

As you can see, the overhead is faster and much more consistent.

Here is one run with the is_string check removed from Normal:
- teststringNormal took: 0.010342836380005
- teststringOverhead took: 0.012849092483521
up
11
bantam at banime dot com
15 years ago
Daniel's typehint implementation was just what I was looking for but performance in production wasn't going to cut it. Calling a backtrace every time hurts performance. For my implementation I didn't use it, after all, PHP tells us what the data type is in the error message, I don't feel I need to evaluate the argument where I am using typehinting. Here is the cut down version I use in my error handling class:

<?php
       
public static function typehint($level, $message)
        {
            if(
$level == E_RECOVERABLE_ERROR)
            {
                if(
preg_match('/^Argument (\d)+ passed to (?:(\w+)::)?(\w+)\(\) must be an instance of (\w+), (\w+) given/', $message, $match))
                {
                    if(
$match[4] == $match[5])
                        return
true;
                }
            }
           
            return
false;
        }
?>

Hope this can be of use to somebody.
up
4
jesdisciple @t gmail -dot- com
16 years ago
The manual's sample code says:
<?php
//...
// Fatal Error: Argument 1 must not be null
$myclass->test(null);
//...
?>

And this is true, unless a default value of NULL is given; in fact, this is the only way to give a default value for object arguments (as a default value must be a constant expression):
<?php
$mine
= new MyClass();
$mine->test(NULL);
class
MyClass{
    public function
__construct(OtherClass $arg = NULL){
        if(
is_null($arg)){
           
//Apply default value here.
       
}
    }
    public function
test(array $arr = NULL){
       
print_r($arr);
    }
}
class
OtherClass{
   
}
?>
up
3
doom at doom dot pl
10 years ago
I've done some tests of the overhead that class Typehint  gives us.
At my PC it goes as follows:
teststringNormal took: 0.041965961456299
teststringOverhead took: 0.48374915122986
It's like 10x longer time (not mention about memory usage), it's just because exception is thrown EVERY SINGLE TIME, along with expensive preg_match() and debug_backtrace() calls.
I think that using class in bigger applications will increase overhead like 100% or more.
<?php

function teststringOverhead(string $string) {
    return
$string;
}
function
teststringNormal($string){
    if(!
is_string($string)){
        return;
    }
    return
$string;
}
$loopTimes = 20000;

/////////// test of overhead implementation vs normal
$t1 = microtime(true);
for(
$i = 0; $i <= $loopTimes; $i++)  teststringNormal("xxx");
echo
"<br>teststringNormal took: " . (microtime(true) - $t1);

$t2 = microtime(true);
for(
$i = 0; $i <= $loopTimes; $i++)  teststringOverhead("xxx");
echo
"<br>teststringOverhead took: " . (microtime(true) - $t2);
?>
up
3
sorin dot badea91 at gmail dot com
11 years ago
I've implemented a basic function to ensure argument's type.

<?php
/**
* Primary types
*/
class Type
{
    const
SKIP     = 0;
    const
INT      = 1;
    const
STRING   = 2;
    const
BOOLEAN  = 3;
    const
CALLBACK = 4;
    const
FLOAT    = 5;
    const
RESOURCE = 6;
}

/**
* @throws InvalidArgumentException
*/
function ensureType()
{
   
$debugStack = debug_backtrace();
   
$argv       = $debugStack[1]['args'];
   
$types      = func_get_args();
    foreach (
$argv as $key => $value) {
       
$message = null;
        if (
is_null($value)) {
            continue;
        }
        switch (
$types[$key]) {
            case
Type::INT:
                if (!
is_int($value)) {
                   
$message = 'Argument ' . $key . ' passed to ' . $debugStack[1]['function'] . '() must be of type int';
                }
                break;
            case
Type::STRING:
                if (!
is_string($value)) {
                   
$message = 'Argument ' . $key . ' passed to ' . $debugStack[1]['function'] . '() must be of type string';
                }
                break;
            case
Type::BOOLEAN:
                if (!
is_bool($value)) {
                   
$message = 'Argument ' . $key . ' passed to ' . $debugStack[1]['function'] . '() must be of type boolean';
                }
                break;
            case
Type::CALLBACK:
                if (!
is_callable($value)) {
                   
$message = 'Argument ' . $key . ' passed to ' . $debugStack[1]['function'] . '() must be a valid callback';
                }
                break;
            case
Type::FLOAT:
                if (!
is_float($value)) {
                   
$message = 'Argument ' . $key . ' passed to ' . $debugStack[1]['function'] . '() must be of type float';
                }
                break;
            case
Type::RESOURCE:
                if (!
is_resource($value)) {
                   
$message = 'Argument ' . $key . ' passed to ' . $debugStack[1]['function'] . '() must be of type resource';
                }
                break;
        }
        if (!
is_null($message)) {
            if (
is_object($value)) {
               
$message .= ', instance of ' . get_class($value) . ' given';
            } else {
               
$message .= ', ' . gettype($value) . ' given';
            }
            throw new
InvalidArgumentException($message);
        }
    }
}

function
dummyFunction($var1, $var2, $var3)
{
   
ensureType(Type::BOOLEAN, Type::INT, Type::STRING);
}

$object = new ReflectionClass('ReflectionClass');

dummyFunction(1, $object, 'Hello there');
up
1
wickedmuso at gmail dot com
16 years ago
One useful thing with Type Hinting that I could not find in the documentation (but tested) is that you can also use an Interface in the hint (versus a Class).  This is a very useful tool if you are trying to code to Interfaces rather than Classes (which is common in Test Driven Development and Dependency Injection paradigms).  It means your external class can present itself into the method as long as it implements the nominated Interface (obviously).
up
1
wbcarts at juno dot com
16 years ago
TYPE-HINTING and VISIBILITY

Type-hinting is just one more small piece of PHP that protects our objects when visibility cannot.

<?php

class Point {
  public
$x, $y;

  public function
__construct($xVal = 0, $yVal = 0) {
   
$this->x = $xVal;
   
$this->y = $yVal;
  }
}

class
Polyline {
  protected
$points = array();

  public function
addPoint(Point $p) {  // the line we're interested in...
   
$this->points[] = $p;
  }
}

$point1 = new Point(15, 12);
$polyline = new Polyline();
$polyline->addPoint($point1);
$polyline->addPoint(new Point(55, 22));
$polyline->addPoint(new Point(33, 31));

$polyline->addPoint(new stdClass());    // PHP will throw an error for us! 

?>

Since our Polyline::addPoint() function has to be public, any outside code can try to pass anything. But, when type-hinting is declared, PHP throws an error when phoney data tries to sneak by.
up
0
ASchmidt at Anamera dot net
5 years ago
Sometimes you want to work with classes (not their instances), but Typehinting is only permitted for an object (instance).

Here is sample code that tests whether the parameter is a valid type (the class/subclass/instance of an expected class):

<?php
declare(strict_types=1);

class
MyClass {};

function
myfunc( $class ) {
   
// Validate classname.
   
if ( $class instanceof MyClass ) {
       
$class = get_class( $class );
    } elseif ( !
class_exists( $class ) or !is_a( $class, MyClass::class, true ) ) {
        throw new \
TypeError( "$class is not of type " . MyClass::class );  
    }
   
// Proceed with processing of MyClass subclass ...
}

$myclass = new MyClass;
myfunc( $myclass );                // valid
myfunc( MyClass::class );        // valid
myfunc( \String::class );          // not of type MyClass.
up
0
mrice at rcs dot us
8 years ago
In regards to http://php.net/manual/en/language.oop5.typehinting.php#103729 , when using PHP in Eclipse, the best way to typehint is already available in Eclipse natively.

The format is /* @var $variable ObjectName */

The single /* is important. You can also use namespaces, IE

/* @var $variable \Some\Namespace */

In short, there is no reason to create functions that return itself.
up
-1
Anonymous
9 years ago
i think this is pretty close.

$lines=hhb_mustbe('array',file("foofile"));
//either file succeeds, and returns an array, or file returns FALSE which is not an array, which throws an unexpectedValueException.

$socket=hhb_mustbe('Socket',socket_create(AF_INET,SOCK_STREAM,getprotobyname('tcp')));
//either socket_create succeeds, and returns a Socket, or socket_create returns False, which is not a resource of type Socket, and you get an UnexpectedValueException

$size=hhb_mustbe('int',filesize(somefile));
//either filesize() returns an integer, or returns FALSE wich is not an int, and you'll get UnexpectedValueException.

    function hhb_mustbe(/*string*/$type,/*mixed*/$variable){
        //should it be UnexpectedValueException or InvalidArgumentException?
        //going with UnexpectedValueException for now...
        $actual_type=gettype($variable);
        if($actual_type==='unknown type'){
            //i dont know how this can happen, but it is documented as a possible return value of gettype...
            throw new Exception('could not determine the type of the variable!');
        }
        if($actual_type==='object'){
            if(!is_a($variable,$type)){
                $dbg=get_class($variable);
                throw new UnexpectedValueException('variable is an object which does NOT implement class: '.$type.'. it is of class: '.$dbg);
            }
            return $variable;
        }
        if($actual_type==='resource'){
            $dbg=get_resource_type($variable);
            if($dbg!==$type){
                throw new UnexpectedValueException('variable is a resource, which is NOT of type: '.$type.'. it is of type: '.$dbg);
            }
            return $variable;
        }
        //now a few special cases
        if($type==='bool'){
            $parsed_type='boolean';   
            } else if($type==='int'){
            $parsed_type='integer';
            } else if($type==='float'){
            $parsed_type='double';
            } else if($type==='null'){
            $parsed_type='NULL';
            } else{
            $parsed_type=$type;
        }
        if($parsed_type!==$actual_type && $type!==$actual_type){
            throw new UnexpectedValueException('variable is NOT of type: '.$type.'. it is of type: '.$actual_type);
        }
        //ok, variable passed all tests.
        return $variable;
    }
up
0
gdecad at NOSPAM dot example dot com
14 years ago
I have made a little bench between three method of type hinting for native type (string, integer, ...).

First method : by test type in function like :
<?php
function testTest($arg) {
    if (!
is_string($arg)) {
       
trigger_error('Argument $arg passed to test must be an instance of string, other given');
    }
    return
$arg;
}
?>

Second method : by object representing native type :
<?php
function testObject(StringObj $arg) {
    return
$arg;
}
?>

Third method : by class TypeHint proposed by Daniel :
<?php
function testHint(string $arg) {
    return
$arg;
}
?>

the results are here :
bench for 100000 iterations,  in seconds
        avg                    min                    max                    total
test    5.3275489807129E-6    2.8610229492188E-6    0.0033020973205566    0.53275895118713
object    4.9089097976685E-6    3.814697265625E-6    0.0025870800018311    0.49089503288269
hint    3.2338891029358E-5    2.9802322387695E-5    0.0025920867919922    3.2338931560516

As you can see, the method by object is the best
now you know...
up
0
dpariyskiy at netatwork dot com
15 years ago
Daniel, thank you for the type hinting class. Very useful.
For anyone having a problem where php isn't setting Daniel's error handler, as was the case for me, try changing initializeHandler function to the following:

<?php
public static function initializeHandler()
{
   
   
set_error_handler(array('Typehint','handleTypehint'));
   
    return
TRUE;
}
?>

Hope this helps,
--Dmitriy
up
0
DanielLWood [at] Gmail [dot] Com
16 years ago
To follow up on my original post dealing with the type hinting class I provided:

Kalkamar is absolutely correct, it is slow and is a hack.  Everyone who uses it and wants to see this type of syntax native needs to post on the 'php internals' development thread in support.

Thanks,

Dan
up
0
marcus at ignorethis netweblogic dot com
16 years ago
Love the typehint object Daniel. Great effort!

However, it still throws catchable fatal errors, which is not what I want, so I added one line to handleTypehint() so it throws an Exception.

<?php
public static function handleTypehint($ErrLevel, $ErrMessage) {
        if (
$ErrLevel == E_RECOVERABLE_ERROR) {
            if (
preg_match ( TYPEHINT_PCRE, $ErrMessage, $ErrMatches )) {
                list (
$ErrMatch, $ThArgIndex, $ThClass, $ThFunction, $ThHint, $ThType ) = $ErrMatches;
                if (isset (
self::$Typehints [$ThHint] )) {
                   
$ThBacktrace = debug_backtrace ();
                   
$ThArgValue = NULL;
                    if (
self::getTypehintedArgument ( $ThBacktrace, $ThFunction, $ThArgIndex, $ThArgValue )) {
                        if (
call_user_func ( self::$Typehints [$ThHint], $ThArgValue )) {
                            return
TRUE;
                        }
                    }
                }
                throw new
Exception($ErrMessage);
            }
        }
        return
FALSE;
    }
?>
up
0
mlovett at morpace dot com
19 years ago
Type hinting works with interfaces too. In other words, you can specify the name of an interface for a function parameter, and the object passed in must implement that interface, or else type hinting throws an exception.
up
-1
john at cast dot com
13 years ago
i use eclipse ganymede as an IDE and it offers "intellisense" where it can, i.e. when variables are "declared" via type hinting or a "new"-statement . i found using the following pattern helps eclipse along as well:

<?php
class MyClass{

  public static function
Cast(MyClass &$object=NULL){
       return
$object;
  }

  public
method CallMe(){
  }
}

$x=unserialize($someContent);
$x=MyObject::Cast($x);
$x->CallMe();
?>

after calling Cast(), due to the type hinting, eclipse offers me the "CallMe" function in a dropdown when i type "$x->" in the code afterwards.

i found this very practical und included the Cast() function in my code template for new classes. i've been wondering, if there is a drawback i oversaw, but i haven't noticed any negative effects so far... maybe some of you will find this just as handy as i do ;o)

p.s. do note: when used in inherited classes a STRICT-notice comes up, because the function definition of the inherited class doesn't match the parent's definition (different type hinting) - but it works great!
up
-1
dr dot mustafa dot darwish at gmail dot com
6 years ago
This matrix demonstrate passing different variable types with different type hints:
<?php

$array
= [10, 10.12, true, '10USD', 'USD'];
function
getValue($x, $y)
{
   
$typeMap = [
       
'integer' => 'int',
       
'int' => 'int',
       
'float' => 'float',
       
'double' => 'float',
       
'real' => 'float',
       
'string' => 'string',
       
'boolean' => 'bool',
       
'bool' => 'bool',
    ];
   
$yType = $typeMap[gettype($y)];
   
$xType = $typeMap[gettype($x)];

    if (
$xType == 'string')
       
$x = "'$x'";
    elseif (
$xType == 'bool')
       
$x = $x ? 'true' : 'false';
    try {
        eval(
"if (!function_exists('test$yType')) {function test$yType($yType \$arg){}} test$yType($x);");
    } catch (
Error $e) {
        return
get_class($e).': '.$e->getMessage();
    }
    return
'Pass';
}

?>
<table border="1" width="50%">
    <thead>

    <tr>
        <td></td>
        <td align="center" colspan="<?= count($array) ?>"><strong>Input value</strong></td>
    </tr>
    <tr>
        <td></td>
        <?php foreach ($array as $header): ?>
            <td><?= gettype($header), ' ', var_export($header) ?></td>
        <?php endforeach; ?>
    </tr>
    </thead>
    <tbody>
    <tr></tr>
    <?php foreach ($array as $y): ?>
        <tr>
            <td><?= gettype($y), ' ', var_export($y) ?></td>
            <?php foreach ($array as $x): ?>
                <td><?= getValue($x, $y); ?></td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
up
-1
info at thewebsiteguy dot com dot au
16 years ago
I find it rather frustrating that PHP has internal data types but doesn't allow optional hinting for it.  I REALLY needed it for something, so I found a way around it.

<?php
abstract class DataType
{
    protected
$length;
    protected
$precision;
    protected
$number;
   
  public function
__construct()
  {
   
$this->number = false;
   
$this->precision = null;
  }
   
    public function
GetLength()
    {
        return
$this->length;
    }
   
    public function
IsNumber()
    {
        return
$this->number;
    }

    public function
GetPrecision()
    {
        return
$this->precision;
    }
}

class
Integer extends DataType
{
    public function
__construct($length = 12)
    {
       
parent::__construct();
       
$this->number = true;
       
$this->length = $length;
       
$this->precision = 0;
    }
}

class
Float extends DataType
{
  public function
__construct($length = 12, $precision = 2)
  {
   
parent::__construct();
   
$this->number = true;
   
$this->length = $length;
   
$this->precision = $precision;
  }
}

class
String extends DataType
{
    public function
__construct($length = 255)
    {
       
parent::__constructor();
       
$this->length = $length;
    }
}
//etc etc through the types...
?>

then later I can do this...

<?php
final class Field
{
    public
$Name;
    public
$Mandatory;
    public
$Hidden;
    public
$ListField;
    public
$Value;
    public
$ForeignClass;
    public
$ReadOnly;
    public
$DataType;
   
    public function
__construct($name, DataType $dataType, $mandatory = false, $listField = true, $value = null, $readOnly = false, BaseCBO $foreignClass = null)
    {
       
$this->Name = $name;
       
$this->DataType = $dataType;
       
$this->Mandatory = $mandatory;
       
$this->ListField = $listField;
       
$this->Value = $value;
       
$this->ReadOnly = $readOnly;
       
$this->ForeignClass = $foreignClass;
    }
}

// ....

class DoSomeStuff
{
    public function
DoGenericThings(Field $field)
    {
        if (
$field->DataType instanceof Integer)
        {
           
// do things for an integer field...
       
}
    }
}
?>
up
-2
dr dot mustafa dot darwish at gmail dot com
6 years ago
the following code demonstrate php7 response to various combination of inputs and type hinting in a simple matrix:
<?php
$array
= [10, 10.12, true, '10USD', 'USD'];
function
getValue($x, $y)
{
   
$typeMap = [
       
'integer' => 'int',
       
'int' => 'int',
       
'float' => 'float',
       
'double' => 'float',
       
'real' => 'float',
       
'string' => 'string',
       
'boolean' => 'bool',
       
'bool' => 'bool',
    ];
   
$yType = $typeMap[gettype($y)];
   
$call = 'test($x)';
    if (
$yType == 'string')
       
$call = 'test(\'$x\')';
   
$template = <<<TEMPLATE_FUNCTION
  if (!function_exists('test')) {function test($yType \$arg){}}
try{
$call;
} catch (Error \$e) {
    return \$e->getMessage();
}
return 'Pass';
TEMPLATE_FUNCTION;
    return eval(
$template);
}

?>
<table border="1" width="50%">
    <thead>

    <tr><td></td><td align="center" colspan="<?= count($array) ?>"><strong>Input value</strong></td></tr>
    <tr>
        <td></td>
        <?php foreach ($array as $header): ?>
            <td><?= gettype($header), ' ', var_export($header) ?></td>
        <?php endforeach; ?>
    </tr>
    </thead>
    <tbody>
    <tr></tr>
    <?php foreach ($array as $y): ?>
        <tr>
            <td><?= gettype($y), ' ', var_export($y) ?></td>
            <?php foreach ($array as $x): ?>
                <td><?= getValue($x, $y); ?></td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
up
-5
alejosimon at gmail dot com
14 years ago
For PHP 5.3 version and namespaces support.

<?php

function phpErrorHandler( $code, $message, $file, $line ) {

    if (
error_reporting() & $code ) {

        if (
$code == E_RECOVERABLE_ERROR ) { // Scalar Type-Hinting patch.

           
$regexp = '/^Argument (\d)+ passed to (.+) must be an instance of (?<hint>.+), (?<given>.+) given/i' ;

            if (
preg_match( $regexp, $message, $match ) ) {

               
$given = $match[ 'given' ] ;
               
$hint  = end( explode( '\\', $match[ 'hint' ] ) ) ; // namespaces support.

               
if ( $hint == $given ) return true ;
            }
        }

        return
false ;
    }
}

set_error_handler( 'phpErrorHandler' ) ;

/************************************/

function typeHintTest( integer $arg1 ) {

   
print_r( $arg1 ) ;
}

typeHintTest( true ) ; // Error throw because not integer type.

?>
To Top