©
本文檔使用 php中文網手册 發布
PHP 5 可以使用类型约束。函数的参数可以指定必须为对象(在函数原型里面指定类的名字),接口,数组(PHP 5.1 起)或者 callable (PHP 5.4 起)。不过如果使用 NULL 作为参数的默认值,那么在调用函数的时候依然可以使用 NULL 作为实参。
如果一个类或接口指定了类型约束,则其所有的子类或实现也都如此。
类型约束不能用于标量类型如 int 或 string 。Traits 也不允许。
Example #1 类型约束示例
<?php
//如下面的类
class MyClass
{
public function test ( OtherClass $otherclass ) {
echo $otherclass -> var ;
}
public function test_array (array $input_array ) {
print_r ( $input_array );
}
}
public function test_interface ( Traversable $iterator ) {
echo get_class ( $iterator );
}
public function test_callable (callable $callback , $data ) {
call_user_func ( $callback , $data );
}
}
// OtherClass 类定义
class OtherClass {
public $var = 'Hello World' ;
}
?>
函数调用的参数与定义的参数类型不一致时,会抛出一个可捕获的致命错误。
<?php
// 两个类的对象
$myclass = new MyClass ;
$otherclass = new OtherClass ;
// 致命错误:第一个参数必须是 OtherClass 类的一个对象
$myclass -> test ( 'hello' );
// 致命错误:第一个参数必须为 OtherClass 类的一个实例
$foo = new stdClass ;
$myclass -> test ( $foo );
// 致命错误:第一个参数不能为 null
$myclass -> test ( null );
// 正确:输出 Hello World
$myclass -> test ( $otherclass );
// 致命错误:第一个参数必须为数组
$myclass -> test_array ( 'a string' );
// 正确:输出数组
$myclass -> test_array (array( 'a' , 'b' , 'c' ));
// 正确:输出 ArrayObject
$myclass -> test_interface (new ArrayObject (array()));
// 正确:输出 int(1)
$myclass -> test_callable ( 'var_dump' , 1 );
?>
类型约束不只是用在类的成员函数里,也能使用在函数里:
<?php
// 如下面的类
class MyClass {
public $var = 'Hello World' ;
}
function MyFunction ( MyClass $foo ) {
echo $foo -> var ;
}
// 正确
$myclass = new MyClass ;
MyFunction ( $myclass );
?>
类型约束允许 NULL 值:
<?php
function test ( stdClass $obj = NULL ) {
}
test ( NULL );
test (new stdClass );
?>
[#1] Anonymous [2015-08-30 13:52:14]
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($type,$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;
}
[#2] doom at doom dot pl [2014-03-06 09:16:03]
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);
?>
[#3] sorin dot badea91 at gmail dot com [2013-04-25 03:05:28]
I've implemented a basic function to ensure argument's type.
<?php
class Type
{
const SKIP = 0;
const INT = 1;
const STRING = 2;
const BOOLEAN = 3;
const CALLBACK = 4;
const FLOAT = 5;
const RESOURCE = 6;
}
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');
[#4] michaelrfairhurst at gmail dot com [2013-02-17 21:46:15]
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
[#5] john at cast dot com [2011-04-29 18:33:51]
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!
[#6] Anonymous [2011-04-14 07:43:46]
Please note, there is a strong typing in PHP.
You have to import one PHP library to your existing projects, and create some classes, in which the scalar values will be automatically wrapped by an autoboxing mechanism known from Java, like so:
<?php
class String extends AutoBoxedObject
{
public $value;
public function __construct($value) {
$this->value = $value;
}
public function __toString() {
return "$this->value";
}
public function toUpperCase() {
return strtoupper($this->value);
}
}
function & string($value = null) {
$x = & VariablesManager::getNewPointer(new String($value));
return $x;
}
?>
From now on you can declare the type of data in your variables, and validate their values in a class constructor.
See this example:
<?php
$y = & string("aaa");
// lets check, that $y is an object
var_dump($y);
// now we are overwritting $y variable with a scalar value of "zzz"
$y = "zzz";
// var_dump() shows us, that "zzz" is still an object, not the scalar type as in regular PHP
var_dump($y);
// the line below should raise a fatal error, because "zzz" was a scalar type (string), but it will be ok, because $y is still an object (thanks to autoboxing)
var_dump($y->toUpperCase());
?>
Now, the output is:
object(String)#1 (2) {
["value"]=>
string(3) "aaa"
["ref":protected]=>
int(1)
}
object(String)#2 (2) {
["value"]=>
string(3) "zzz"
["ref":protected]=>
int(1)
}
string(3) "ZZZ"
As you can see, PHP can be as good as Java or C#:)
To enforce strong data types you just have to do it in the wrapper class, like so:
<?php
class Integer extends AutoBoxedObject
{
public $value;
public function __construct($value) {
if(!is_int($value)) { throw new Exception("Invalid data type"); }
$this->value = $value;
}
public function getValue() {
return $this->value;
}
public function __toString() {
return (string)$this->value;
}
}
?>
To download the library, just google for 'strong data typing autoboxing' . There is an article describing how to use it, and a download link to the appropriate PHP library.
[#7] macobex dot exe at gmail dot com [2011-02-10 18:48:22]
I agree [that] using object/wrapper is faster than using the is_* functions.
But I think we must also consider the fact that when we pass an argument to a function, most likely we will use it's value. So I've done a little test to see which approach is faster.
Primitive Approach:
<?php
function testPrimitive($str) {
if ( is_string($str) )
$x = $str . ' world'; // using the argument's value
else
trigger_error('Argument passed must be of type string.');
}
?>
Wrapper Approach:
<?php
function testWrapper(StringWrapper $str) {
$x = $str->getValue() . ' world'; // using the argument's value
}
?>
Test: 10,000 iterations
Results:
primitive average: 0.026113033294678
wrapper average: 0.063039064407349
primitive is faster than wrapper approach by 41.42357368431%
Using the is_* function is faster than using wrapper class, given the fact that we just not want to check the type of the argument, but we also want to use the value of the argument.
[#8] alejosimon at gmail dot com [2010-10-22 11:55:14]
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.
?>
[#9] gdecad at NOSPAM dot example dot com [2010-10-22 06:02:49]
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...
[#10] reinier dot kip at gmail dot com [2009-09-15 01:22:32]
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'
?>
[#11] dpariyskiy at netatwork dot com [2009-09-08 08:36:24]
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
[#12] bantam at banime dot com [2009-01-06 02:53:01]
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.
[#13] wickedmuso at gmail dot com [2008-10-24 20:26:35]
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).
[#14] info at thewebsiteguy dot com dot au [2008-10-08 06:30:08]
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...
}
}
}
?>
[#15] wbcarts at juno dot com [2008-10-01 18:20:52]
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.
[#16] DanielLWood [at] Gmail [dot] Com [2008-09-08 17:59:43]
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
[#17] kalkamar at web dot de [2008-09-04 02:24:41]
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.
[#18] comments at ignorethis netweblogic com [2008-07-17 07:39:44]
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.
[#19] marcus at ignorethis netweblogic dot com [2008-07-14 06:18:51]
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;
}
?>
[#20] madness [2008-07-11 03:09:12]
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.
[#21] Daniel dot L dot Wood at Gmail dot Com [2008-05-26 11:00:05]
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..
[#22] jesdisciple @t gmail -dot- com [2007-11-06 19:50:53]
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{
}
?>
[#23] Jazz [2007-10-16 09:20:51]
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*)
[#24] ldebuyst->brutele.be [2007-02-28 06:52:34]
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.
[#25] Nikivich [2007-02-23 07:28:21]
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... :-)
[#26] Edorian [2007-02-07 10:12:19]
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.
[#27] nicholas at nicholaswilliams dot info [2006-11-13 12:53:52]
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
[#28] [2006-09-02 08:59:53]
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.
[#29] mlovett at morpace dot com [2005-07-06 03:54:33]
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.