Thursday , 17 August 2017
Home / Programming / PHP / Type Hinting – PHP OOP Tutorial
Type Hinting
Type Hinting

Type Hinting – PHP OOP Tutorial

This tutorial is a part of the PHP OOP tutorial series. If you would like to view the complete list of tutorials under this series and the order you should follow, then click here. http://jaskokoyn.com/php-oop-tutorial-series/ 

In some cases, you’ll find yourself needing more information passed into your methods. What if you wanted to pass in objects into your methods? This is actually possible with type hinting. Let’s learn how to do this and why it would be useful.

Before we begin, we’re going to create a new class called Apple. Create a file called apple.class.php and insert this bit of code.

Nothing complicated going on here. We just have a class called Apple and it has a property of calories with the value of 100.

Open up your pet.class.php file and add these 2 methods in your class.

Our getEnergy() method simply returns the pet’s current energy. Our eatApple() method is a very special method because it utilizes type hinting.

What is type hinting?

Type hinting allows you to specify the type of data the method is expecting to accept. In our case, we want our eatApple() method to accept an instance of the Apple class. There are a couple of reasons you might want to do this. For one, we don’t want our Pet eating another Pet. We don’t want it eating a desk. We know we want it to eat an Apple. Another example would be if you had a bank and you were going to enroll someone in your bank. You want to enroll someone and not a bank account type or string or an integer.

By accepting objects you gain access to those methods and properties. In order to use type hinting, you first must specify the type of object this is. In our case, we want to use the Apple object. Next, you give it a name to refer to this class.

One last thing, you can not simply pass in the object. You must create an instance and then pass that instance in. Our eatApple() method is simply adding the calories of the apple to it’s energy. Let’s open up our index.php file and update our code to this.

As you can see, we’re including our apple.class.php file before we do anything. This class must be present if we want to use it. Next, we create an instance of the apple and then call the eatApple() method and pass in our instance. Lastly, we call the getEnergy() method to grab the current energy of our pet.

Conclusion

By using type hinting, you’re restricting the values that can be passed into your methods. You also give temporary access to classes inside other classes. If you would like more information about type hinting then go here.

http://php.net/manual/en/language.oop5.typehinting.php

About Jasko Koyn

Check Also

Static Methods and Properties

Static Methods and Properties – PHP OOP Tutorial

Let’s learn how to create static methods and properties. Anything that’s static is basically something …

One comment

  1. Thanks. You give me “clear vision” about type hinting. i’m a beginner in php, specially oop.
    i’m from indonesia, sorry for my bad english.

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link