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.

PHP

1

2

3

4

5

6

<?php

classApple

{

public$calories=100;

}

?>

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

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

1

2

3

4

5

6

7

8

9

publicfunctioneatApple(Apple$apple)

{

$this->energy+=$apple->calories;

}

publicfunctiongetEnergy()

{

return'Current Energy: '.$this->energy.'<br>';

}

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.

PHP

1

2

3

4

5

6

7

8

9

10

11

<?php

include('classes/pet.class.php');

include('classes/CanadianPet.class.php');

include('classes/apple.class.php');

$jack=newPet('Jack');

$apple=newApple();

$jack->eatApple($apple);

echo$jack->getEnergy();

?>

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.