I am having trouble wrapping my head around the importance of Type hinting in PHP.
Apparently 'type hinting' in PHP can be defined as follows:
"Type hinting" forces you to only pass objects of a particular type. This prevents you from passing incompatible values, and creates a standard if you're working with a team etc.
So type hinting at the most basic level, is not required to actually make the code work?
I have the following code to try and understand what is going on...
Index.php
<?php
include 'Song.php';
$song_object = new Song;
$song_object->title = "Beat it!";
$song_object->lyrics = "It doesn't matter who's wrong or right... just beat it!";
function sing(Song $song)
{
echo "Singing the song called " . $song->title;
echo "<p>" . $song->lyrics . "</p>";
}
sing($song_object);
Song.php
<?php
class Song
{
public $title;
public $lyrics;
}
the code does its thing with or without the little type hint in the function sing();
So, this leads me to believe that type hinting is simply a coding convention to make sure only certain classes are used and are not needed to produce functional code, is this correct?
Type hinting, as the quote above suggests, is there to create a standard if you're working with a team.
Am I missing something here?