PHP5 Tutorial - Function - Method Type Hinting in PHP5

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

Your email:  
Subscribe Unsubscribe  

del.icio.us Reddit Slashdot Digg Facebook Technorati Google StumbleUpon Windows Live Furl Netscape Yahoo Bloglines Bookmark.it Ask Spurl Diigo

One Response to “PHP5 Tutorial - Function - Method Type Hinting in PHP5”

  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

Leave a Reply

Top Internet blogs Programming Blogs - Blog Catalog Blog Directory TopOfBlogs Technology blogs Top Blog Topsites List