Restricting acceptable data with type hinting

Author Topic: Restricting acceptable data with type hinting  (Read 375 times)

Offline beingchinmayTopic starter

  • Trade Count: (0)
  • Semi-Newbie
  • *
  • Thank You 0
  • Posts: 34
  • Karma: 0
  • Gender: Male
  • I am Chinmay
    • Fat Cutter
Restricting acceptable data with type hinting
« on: 08-19-2016, 06:33:24 »
Let’s say that you have developed the simple examples in this chapter ready for deployment in an e-commerce application. Such an application is likely to have a class for the shopping cart and a method called addItem() to add the user’s purchases to the cart. To avoid errors, you need to check that the item being added belongs to an acceptable data type. One way of doing so would be to use the instanceof operator like this:

public function addItem($item)
if ($item instanceof Ch2_Product) {
// It's OK, add it to the cart
} else {
// Reject it


Related Topics

  Subject / Started by Replies Last post
2 Replies
Last post 06-19-2010, 04:57:54
by corenicles
24 Replies
Last post 07-06-2014, 23:03:29
by chandanthaver