Restricting acceptable data with type hinting

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

Offline beingchinmayTopic starter

  • Trade Count: (0)
  • Jr. Member
  • **
  • Thank You 1
  • Posts: 73
  • Karma: 0
  • Gender: Male
  • I am Chinmay
    • Modular Kitchen Designer
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
23 Replies
Last post 07-06-2014, 23:03:29
by chandanthaver