search for in the  
<ReflectionExceptions>
Last updated: Thu, 19 May 2005

Type Hinting

PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects by specifying the name of the class in the function prototype.

Example 19-39. Type Hinting example

<?php
// An example class
class MyClass
{
  
/**
     * A test function
     *
     * First parameter must be an object of type OtherClass
     */
  
public function test(OtherClass $otherclass) {
       echo
$otherclass->var;
   }
}

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

Failing to satisfy the type hint results in a fatal error.

<?php
// An instance of each class
$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);
?>

Type hinting also works with functions:

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

Type Hints can only be of the object type. Traditional type hinting with int and string are not supported.



User Contributed Notes
Type Hinting
10-May-2005 09:41
If wrong object is given, error points to method definition instead of place where method has been called. Because of that type hinting is not really useful. Instead use:

<?php

function($foo)
{
 
assert('$foo instanceof ClassRequired');
 
// ...
}

?>

and use your custom error handler that dumps debug_backtrace() -- this will allow you to find caller that passes wrong object. Works in PHP4 as well.
ryo at shadowlair dot info
28-Feb-2005 12:53
Although type-hinting prevents null values from being passed, setting a default null value is not a parse error.
However, calling the function with a null parameter, or calling it without the parameter (thus using the default value) will result in a fatal error:

<?php

function foo(MyClass $object = null) {
  
// this is a valid function definition
}

$o = new MyClass();
foo($o);
foo(null); // fatal error
foo(); // fatal error

?>
caliban at darklock dot com
23-Feb-2005 09:34
In case you're worried, type hinting does allow descendants. Extending the documentation example:

<?php
  
// Example class
  
class MyClass
  
{
     public function
test(OtherClass $otherclass)
     {
         if(
is_callable(array($otherclass,$otherclass->var)))
         {
            
$otherclass->{$otherclass->var}();
         }
         else
         {
             echo
$otherclass->var;
         }
     }
   }

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

  
// Yet another example class
  
class DerivedClass extends OtherClass
  
{
     function
__construct()
     {
        
$this->var="Planet";
     }

     public function
Planet()
     {
         echo
"Hello ".$this->var;
     }
   }

  
$myclass = new MyClass;
  
$otherclass = new OtherClass;
  
$derivedclass = new DerivedClass;

  
// Works - prints "Hello World"
  
$myclass->test($otherclass);

  
// Works - calls DerivedClass::Planet()
   //    which prints "Hello Planet"
  
$myclass->test($derivedclass);
?>
Tim Ansell
10-Feb-2005 07:28
Type hinting also prevents null from been given.

<ReflectionExceptions>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT