Question

I need help understanding type hinting with objects. I tried searching stackoverflow but cannot find anything that has another user explain its use. If you find one let me know. First let me explain what I do understand.

When using a type hint of array the user must type in a parameter that is an array otherwise its going to throw an error.

<?php
function something(array $myval)
{
    return print_r($myval);
}

When i try it with an object i get an error. I might be writing it wrong, but please help me understand how to write it.

<?php
class Person
{
    function name($name)
    {
        return $name;
    }
}

$foo = new Person();

function doSomething(Person $lname)
{
    return $lname->name;
}

doSomething('smith');

From what I understand when a function is type hinted of object Person (in this example) the parameter variable will have access to the objects methods just like when you instantiate an object and echo out its methods. I can be wrong but please correct me. My other question is that if this is true where a Person parameter has access to the Person methods what makes this any different from just instantiating the Person class and manually echoing out the methods.

Was it helpful?

Solution

Using your example:

$foo = new Person;
$foo->name = 'smith';

$something = doSomething($foo);

echo $something;

Type hinting means that whatever you pass must be an instance of (the same type as) the type you're hinting.

So, if you hint to Person only objects of that type will be accepted.

In the example you gave, you tried to pass a string instead of an object.

Update

"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, let's say you have a function sing(). You want to be sure that it will only accept objects of type Song.

Let's create our class Song:

class Song{

public $title;
public $lyrics;

}

and our function sing(). We will type hint to Song to ensure that no other type of params can be passed to it:

function sing(Song $song){

echo "Singing the song called " .$song->title;
echo "<p>" . $song->lyrics . "</p>";

}

Now, again, the function can ONLY accept objects of type Song because that's what we hinted to in the declaration (Song $song).

Let's create a Song and pass it:

$hit = new Song;

$hit->title = "Beat it!";
$hit->lyrics = "It doesn't matter who's wrong or right... just beat it!";

then we call:

sing($hit);

Which will work just fine.

Now, let's say we have a class Poem:

class Poem{

public $title;
public $lyrics;

}

$poem = new Poem;

$poem->title = "Look at the sea";
$poem->lyrics = "How blue, blue like the sky, in which we fly..."

If we try to call it using our function 'sing';

sing($poem)

we will get an error because $poem is not the type of object we've hinted to when creating the function sing().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top