Type Hinting

Type Hinting

Type Hinting is a new concept introduced in PHP5,
Which is forcing the function parameter to be and object or an array.
A parameter can be forced to be an object by specifying the name
of the class in function prototype

Eg

<?php

Class ClassA
{
function get_values(ClassB $obj_class_b)
{
…………..
}
}

Class ClassB
{
public var $a = ‘hello’;
}

$obj_class_a = new ClassA;
$obj_class_b = new ClassB;

$obj_class_a = get_values(’hai’);
//output: Fatal Error: Argument 1 must be an object of class ClassB

$obj_class_a = get_values(’hai’);

$foo = new stdClass;
$obj_class_a = get_values($foo); /// Sending object of some other class

//output: Fatal Error: Argument 1 must be an instance of ClassB

$obj_class_a->get_values(null);
// output: Fatal Error: Argument 1 must not be null

$obj_class_a->get_values($obj_class_b);
/// will work fine

?>

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.