1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Dear Visitor, we are presently having a membership drive and as a result EVERY new member that registers an account and makes THREE meaningful posts will have a mighty $10,000$ forum credits added to their wallets ... GET YOURS NOW!
    Dismiss Notice
  3. IMSpot.Org is under new management and we look forward to bringing you a range of exciting enhancements in coming days.
    Dismiss Notice
  4. Dear Visitors, why not register an account now and say hello ... we really would love to hear from you?
    Dismiss Notice
  5. Welcome to IMSpot.Org an internet marketing forum and very friendly place for discussion, learning and help in your online endeavours ... please consider joining our internet marketing community and sharing your knowledge with our members.
    Dismiss Notice

CLASS TYPE HINTS IN FUNCTION PARAMETERS

Discussion in 'PHP' started by beingchinmay, Dec 29, 2015.

Tags:
  1. beingchinmay

    beingchinmay An Active Forum Member

    Joined:
    Jun 24, 2015
    Messages:
    28
    Likes Received:
    1
    Although PHP is not a strictly typed language in which you would need to declare what type your variables are, it does allow you (if you wish) to specify the class you are expecting in your function’s or method’s parameters.

    Here’s the code of a typical PHP function, which accepts one function parameter and first checks if it belongs to the class it requires:

    Writing code that verifies the object’s type in each relevant function can be a lot of work. To save you time, PHP enables you to specify the class of the parameter in front of the parameter itself.

    Following is the same example using class type hints:


    When the function is called, PHP automatically performs an instanceof check before the function’s code starts executing. If it fails, it will abort with an error. Because the check is an instanceof check, it is legal to
    send any object that satisfies the is-a relationship with the class type. This feature is mainly useful during development, because it helps ensure that you aren’t passing objects to functions which weren’t designed to handle them.
     
Loading...
Live Thread addon by sonnb

Share This Page