diff --git a/language/oop5/typehinting.xml b/language/oop5/typehinting.xml index 7c32cee49a..ea85ec35fc 100644 --- a/language/oop5/typehinting.xml +++ b/language/oop5/typehinting.xml @@ -1,9 +1,9 @@ - + Type Hinting - PHP 5 introduces Type Hinting. Methods are now able to force parameters + 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. @@ -12,6 +12,7 @@ +]]> + + + Failing to satisfy the type hint results in a fatal error. + + +test('hello'); -// Argument 1 must be an instance of OtherClass + +// Fatal Error: Argument 1 must be an instance of OtherClass $foo = new stdClass; $myclass->test($foo); -// Argument 1 must not be null + +// Fatal Error: Argument 1 must not be null $myclass->test(null); -// This is fine -$foo = new OtherClass; -// Prints Hello World -$myclass->test($foo); -?> +// Works: Prints Hello World +$myclass->test($otherclass); +?> +]]> + + + Type hinting also works with functions: + + +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. +