Home > PHP, PHP Tutorials, PHP5 OOPS Tutorials > PHP5 Tutorial – Function – Method Type Hinting in PHP5

PHP5 Tutorial – Function – Method Type Hinting in PHP5

November 14th, 2007 admin Leave a comment Go to comments

PHP5 Introduces Method Type Hinting. Type Hinting allows a function to force parameters to be objects of a particular class by specifying the name of the class in the function prototype.

Type Hinting is optional in all cases except catch block.




Look at the example below without Method Type Hinting

class Customer {
	...
}
 
class Order {
   public function myfunc($c)
   {
	...
   }
}
 
$o = new Order();
$o->myfunc(“any data”);

In the above code on line number starting from 6 to 9, the method myfunc() accepts any type of parameter and on line 13 we pass it a string data. However, I as the designer of the class wanted the developer to pass a Customer type of object to the function myfunc(), but because I did not specify the type in the function; any data can be passed. Method type hinting has been introduced to avoid such type issues.

Example of Method Type Hinting

class Customer {
	...
}
 
class Order {
   public function myfunc(Customer $c)
   {
	...
   }
}
 
$o = new Order();
$o->myfunc(“any data”); //gives error as it expects Customer Object
 
$c = new Customer();
$o->myfunc($c); //works fine as $c is a Customer object

The above code is similar to the previous code, except that the only change is on line number 6. Take your time to compare the difference. The myfunc() now accepts a parameter of type Customer only. Therefore, line number 13 gives an error and line number 16 works well as $c is a customer object.




In the next PHP5 tutorial you will learn about the $this variable.

Feel free to write comments if you need more examples or if you need to ask a question on PHP5 Class. You can also subscribe to my notification service to be informed as an when a new tutorial article goes online. Subscribe Below

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – Magic Methods – __toString() method
  9. PHP5 Tutorial – Magic Methods – __get() and __set()
  10. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  11. PHP5 Tutorial – Magic Methods – __call() method
  12. PHP5 Tutorial – Magic Methods – __autoload() method
  13. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  14. PHP5 Tutorial – Magic Methods – __clone() method

All organizations want to hire skilled, knowledgeable, and competent professionals. If you are an IBM 000-974 certified or Microsoft 70-351 certified professional, you do not need to worry about job. Most job vacancies remain open for the Certified Information Systems Security Professional (CISSP) and Cisco 642-974 DCNIS professionals. Do not late, just enrolled yourself for HP0-J24 exam and secure your future today.

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Stephan
    November 17th, 2007 at 17:54 | #1

    I suggest to add what happens if you call a function with type hinting supplying wrong paramters,
    eg.
    function testFunction(Db $db) { … }

    testFunction(“thisIsAStringNotAnObjectOfTypeDb”);

    -> it causes a fatal error.

    Furthermore, to stress this a bit:
    - up to version 5.0 typehinting only works for objects, not for “array”, “string”, “int” and so on
    - starting from version 5.1: “array” is allowed too

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )