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.
Type hinting is a concept that provides hints to function for the expected data type of arguments. For example, If we want to add an integer while writing the add function, we had mentioned the data type (integer in this case) of the parameter.
Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. Here's an example of adding type information to a function.
Introduction to Python type hints It means that you need to declare types of variables, parameters, and return values of a function upfront. The predefined types allow the compilers to check the code before compiling and running the program.
Since PHP 5 you can use type hinting to specify the expected data type of an argument in a function declaration. When you call the function, PHP will check whether or not the arguments are of the specified type. If not, the run-time will raise an error and execution will be halted.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With