PHP 5 führt Type Hinting ein. Funktionen sind damit fähig, Parameter zu zwingen, Objekte (indem man den Namen der Klasse im Funktionsprototyp spezifiziert) oder Arrays (seit PHP 5.1) zu sein. Wird dabei NULL als Vorgabewert für einen Parameter angegeben so ist dies ein weiterer gültiger Aufrufwert neben dem spezifizierten Typ.
Beispiel #1 Type Hinting Beispiele
<?php
// Eine Beispielklasse
class MyClass
{
/**
* Eine Testfunktion
*
* Der erste Parameter muss ein Objekt des Typs OtherClass sein
*/
public function test(OtherClass $otherclass) {
echo $otherclass->var;
}
/**
* Eine weitere Testfunktion
*
* Der erste Parameter muss ein Array sein
*/
public function test_array(array $input_array) {
print_r($input_array);
}
}
// Eine weitere Beispielklasse
class OtherClass {
public $var = 'Hallo Welt';
}
?>
Wird der Type Hint nicht erfüllt, führt dies zu einem abfangbaren fatalen Fehler.
<?php
// Eine Instanz jeder Klasse
$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);
// Funktionier: Gibt Hallo Welt aus
$myclass->test($otherclass);
// Fatal Error: Argument 1 must be an array
$myclass->test_array('a string');
// Funktioniert: Gibt das Array aus
$myclass->test_array(array('a', 'b', 'c'));
?>
Type Hinting funktioniert ebenfalls mit Methoden
<?php
// Eine Beispielklasse
class MyClass {
public $var = 'Hallo Welt';
}
/**
* Eine Testfunktion
*
* Der erste Parameter muss ein Objekt vom Typ MyClass sein
*/
function MyFunction (MyClass $foo) {
echo $foo->var;
}
// Funktioniert
$myclass = new MyClass;
MyFunction($myclass);
?>
Type hinting mit möglichen NULL Werten:
<?php
/* Akzeptiert NULL Werte */
function test(stdClass $obj = NULL) {
}
test(NULL);
test(new stdClass);
?>
Type Hints können nur vom Typen object und (seit PHP 5.1) array sein. Traditionelles Type Hinting mit int und string wird nicht unterstützt.
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.
?>
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...
Regarding "simple" type hints, you may use SPL: http://pl.php.net/manual/en/book.spl-types.php
Please note that when using the type hinting class together with namespaces, you're asking for trouble:
<?php
namespace a {
interface A {
public function execute(string $sth);
}
}
namespace a\b {
class B implements a\A {
public function execute(string $sth){} // Wrong
public function execute(a\string $sth){} // Correct
}
} // Causes a fatal error on this line. 'string' is considered an object from the namespace 'a'
?>
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
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.
It has already been mentioned here that you can use 'self' as a typehint (in methods of classes and interfaces, not in functions of course), which is useful in interfaces and abstract method declarations to force the implementor to accept an instance of it's own type:
<?php
interface Foo
{
public function baz(self $object);
}
class Bar implements Foo
{
public function baz(self $object)
{
//
}
}
?>
What has not been mentioned by now is that you can use 'parent' as a typehint too. Example with an interface:
<?php
interface Foo
{
public function baz(parent $object);
}
class Baz {}
class Bar extends Baz implements Foo
{
public function baz(parent $object)
{
//
}
}
?>
Bar::baz() will now accept any instance of Baz.
If Bar is not a heir of any class (no 'extends') PHP will raise a fatal error:
'Cannot access parent:: when current class scope has no parent'.
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).
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...
}
}
}
?>
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.
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
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.
Note that you cannot add a type hint and give a default value, apart from arrays. You will get an internal server error, or fatal error.
e.g.
<?php
//Wont work
function test(ObjName $obj = ''){
//.....
}
//Will work
function test(Array $obj = array()){
//.....
}
?>
Even if you have Daniel's implementation of type hinting, a string typehint will still not work if you give it an empty string default too.
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;
}
?>
I must admit that Daniel's implementation is quite awesome (after all we'd be omonyms if I was english, omen nomen ;-) ), for everyone that already has a function/class to handle errors, this is a quick method to integrate the TypeHint class:
<?php
public static function handleError($errno, $errstr, $errfile, $errline){
// Implements just-in-time classes for broad type hinting
if (TypeHint::handleTypehint($errno, $errstr)){
return true;
}
// do your usual stuff here
/*
* ...
*/
}
?>
The initializeHandler method and the Typehint::initializeHandler(); call are rendered useless in this case. Enjoy.
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..
Correction to previous note:
"Failing to satisfy the type hint results in a catchable fatal error."
This only seems to be the case for PHP >= 5.2.0.
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{
}
?>
To Nikivich and Edorian:
There are many times when you would use an equals() method other than to find out if the two objects are the same object. Think of all the primitive wrapper classes in Java, for example -- if you create two new Integer()'s with identical values, equals() returns true, even though they are two different objects. There would be no reason to allow someone to perform an equals() between an Integer and, say, a GregorianCalendar -- it just doesn't make sense. In Java you would attempt this and probably get a ClassCastException, but in PHP no such facility exists, so the best way to prevent this would be through type hinting.
The point Nicholas was making is that you can't specify a stricter type hint on an inherited method, and despite your arguments, that would be a truly useful thing to be able to do.
(True overloading would be a better way, IMHO, but *shrug*)
In reply to Nikivich and Edorian:
Although it isn't quite clear from his post, I believe that the point nicholas is trying to make is that, if you typehint an abstract function, you MUST use that same typehint for all classes extending the abstract class.
As his example shows, if you typehint (Object $object), then you must use the exact same typehint in the extending class. Using the typehint (Table $table) or (Chair $chair) will give fatal errors, even if Table and Chair are subclasses of Object.
In other words, type hinting allows for descendants, as caliban at darklock dot com has shown, except when you're subclassing.
See http://bugs.php.net/bug.php?id=36601 for a bit more info. Flagged as wontfix, though, so something to keep in mind.
In reply to Nicolas
I don't think you exactly understand the inheritance principles
If you want to do the equals thing in a decent OO way, you would do something like this:
class Object {
public equals(Object &o) {
return this == &o; //perform default equals check, one could arguably say that === is the correct default, but doesnt matter for the example
}
}
class Chair extends Object {
}
class Table extends Object {
}
$chair = new Chair();
$table = new Table();
$chair->equals($table); //will print false (zero)
This is actually a correct implementation of an equals method. Since you want to take a chair for example and just call equals() on it WITH ANY OBJECT, you should only hint Object, not an implementation, since the whole point of the equals method is to find out whether it is actually the same object :-) I want to be able to pass a table (which implements Object too, so is perfectly allowed as a parameter to equals).
Hope this clears it up a bit for you... :-)
In response to nicholas at nicholaswilliams dot info:
Of course this doesn't work. Not in Php nor in Java.
You can't put a Chair into Table just because there both implementing "Object"
It wouldn't make any sense to say "i'm expecting an argument that implements the same object that i'm implementing" with type hinting.
You say: "I'm expection an Object of that Class or a Object of a Subclass of that Class " like you do in every OO languange.
Please note that the following will not work:
<?php
abstract class Object
{
public abstract function toString( );
public abstract function equals( Object &$o );
}
class Chair extends Object
{
public function toString( )
{
return 'This is a chair.';
}
public function equals( Chair &$o )
{
return TRUE;
}
}
class Table extends Object
{
public function toString( )
{
return 'This is a table.';
}
public function equals( Table &$o )
{
return TRUE;
}
}
$chair = new Chair();
$table = new Table();
echo $chair->equals( $table );
?>
The expected output is "Fatal error: Argument 1 passed to Chair::equals() must be an instance of Chair, called in [filename] on line 38 and defined in [filename] on line 16" but instead you get "Fatal error: Declaration of Chair::equals() must be compatible with that of Object::equals() in [filename] on line 20".
This is unlike other OO languages (secifically Java) which not only allow but expect this type of code. It is in the nature of abstraction. However, you can get similar results using the following code instead:
<?php
abstract class Object
{
public abstract function toString( );
public abstract function equals( self &$o );
}
class Chair extends Object
{
public function toString( )
{
return 'This is a chair.';
}
public function equals( self &$o )
{
return TRUE;
}
}
class Table extends Object
{
public function toString( )
{
return 'This is a table.';
}
public function equals( self &$o )
{
return TRUE;
}
}
$chair = new Chair();
$table = new Table();
echo $chair->equals( $table );
?>
This code gives the expected result "Fatal error: Argument 1 passed to Chair::equals() must be an instance of Chair, called in [filename] on line 38 and defined in [filename] on line 16". This is the proper behavior but isn't the most intuitive approach for those of us used to OO programming.
Hope this helps someone :-).
Nicholas
The type hinting system can also be used for interfaces. Example:
<?php
interface fooface
{
public function foo ();
}
class fooclass implements fooface
{
public function foo ()
{
echo ('foo<br>');
}
}
class barclass implements fooface
{
public function foo ()
{
echo ('bar<br>');
}
}
class bazclass implements fooface
{
public function foo ()
{
echo ('baz<br>');
}
}
class quuxclass
{
public function foo ()
{
echo ('quux<br>');
}
}
function callfoo (fooface $myClass)
{
$myClass -> foo ();
}
$myfoo = new fooclass;
$mybar = new barclass;
$mybaz = new bazclass;
$myquux = new quuxclass;
callfoo ($myfoo);
callfoo ($mybar);
callfoo ($mybaz);
callfoo ($myquux); // Fails because the quuxclass doesn't implement the fooface interface
?>
Using this syntax you can allow a function to work with different classes as long as they all implement the same interfaces. An example might be an online shop that implements a plugin system for payment. If the creator of the script provides a payment module interface then functions can check if it has been implemented in a given payment class. This means that the details of the class are unimportant, so it doesn't matter if it interfaces with PayPal, HSBC, ProTX or any other payment system you care to name, but if it doesn't properly provide all the functionality a payment module requires a fatal error is generated.
Unfortunately, it doesn't seem possible to use type hinting with new. In java you could do a "fooface myfoo = new fooclass" which would fail if you tried it with quuxclass instead, but as far as I can tell you can't do a similar test on create with PHP.
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.
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);
?>