PHP Type Hinting

王林
Release: 2024-08-29 13:03:37
Original
373 people have browsed it

The PHP Type Hinting is one of the best concepts which you will encounter after the PHP 5 version only. We all know that in PHP Program, we don’t enter the specific data type for any function declaration, variable declaration or any other type of declaration anywhere inside of the PHP Program using PHP Programming Language. But after the PHP 5 and PHP 5+ versions, data types can be passed in the PHP Program as the arguments of the PHP functions. We call this capability as type hinting. It is very helpful at the time of need to change a particular instance at a particular time.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

function printMenu1(Controller, $controller1){
//program statements
…
}
Copy after login

Explanation for the above syntax:

In the above-mentioned syntax, the $controller1 variable is hinted with class name “Controller” but the printMenu1() will only accept the Controller Class Object as the Controller argument.

How Type Hinting works in PHP?

The PHP Type Hinting works for only some type of data. For functions arg/argument, the user will specify some user-defined class instance and it will be hinted by the class name. It works after the PHP 5 version. It works by specifying the data type for variables, functions, etc. after the PHP 5 and PHP 5+ versions. Before PHP 5 versions the PHP Type Hinting concept doesn’t even work.

It is also available or applicable for interfaces, arrays, and functions that are callable. It is not capable of handling specific objects/objects which are like float, int, string, and Boolean type, etc. Suppose if we use PHP Type Hinting for INT data type then the execution of the program will be stopped and leads to an error like “Catchable fatal error” etc..

It helps in specifying the data types which are expected. Those data types are objects, arrays, and interfaces, etc. for a particular argument in a specific function declaration. It is one of the most advantageous things because it will provide better code results and improves by reducing error messages or improves error messages too.

Examples of PHP Type Hinting

Examples of implementing the Type Hinting concept.

Example 1

Here calcNumMilesOnFullTank1() will accept $models1 Object. Then foreach loop is used to access each and every item from the models class object/array term. Then echo statement is used t5o access the first item “item[0]”. But this PHP program will return Fatal error in the output. Here only one string value is added to the function instead of passing many array elements etc. Here item[1] and item[2] have to be multiplied and have to provide the result but provide the fatal error output.

Syntax:

<?php
// The function will only get array as an argument.
function calcNumMilesOnFullTank1(array $models1)
{
foreach($models1 as $item1)
{
echo $carModel1 = $item1[0];
echo " : ";
echo $numberOfMiles1 = $item1[1] * $item1[2];
echo "<br />";
}
}
calcNumMilesOnFullTank1("Toyota");
?>
Copy after login

Output:

PHP Type Hinting

Example 2

Here integerTest1() function accepting the class object “val1”. This is the example of implementing the Type Hinting Concept of the PHP Programming Language. Here inside of the PHP Programming tags a function is created then echo statement is created to print the value of the variable “val1”. Then after closing the parenthesis of the function, integerTest1(12) is called to pass the integer value “12” to the function and then PHP tag is closed. After compiling this PHP Program one will get the output as “12” because integer value “12” is passed to the function’s object.

Syntax:

<?php
function integerTest1(int $val1) {
echo $val1;
}
integerTest1(12);
?>
Copy after login

Output:

PHP Type Hinting

Example 3

This is an example of implementing the Array Type Hinting of the PHP Programming Language. Here at first showColors1 function is created with an array class object $colors1 then after that echo statement is used to print some string test. Then FOREACH concept is used to print the colors1 values which are placed inside of the array/arrays. Then ECHO statement is created to print the col1 values of the colors1 array. Then function’s parenthesis are closed and then $colors1 array variable is created to store some of the string values. They can be colors or any other. One can place any type of elements/items inside of the specific array based on our requirement. Then showColors1() is called to run the whole function/method statements. Then PHP tags closed. Check out the output and know what are the elements going to print.

Syntax:

<?php
function showColors1(array $colors1)
{
echo "List of the colors=>";
foreach($colors1 as $col1)
{
echo "\n".$col1;
}
}
$colors1 = ['Red','Violet','Green','orange','Blue','Yellow'];
showColors1($colors1);
?>
Copy after login

Output:

PHP Type Hinting

Example 4

This is the PHP Program of implementing the PHP Type Hinting. At first, a class called pavankumarsake is created and then inside of the class add() function is created and had 2 parameters. They are a and b variables and then the sum of those two will be printed/returned. Then closing of the parenthesis is done and then a new class is created to store the value of the two variables. Then calc1 and test 1 variable are created to call pavankumarsake() and test1() classes. This program will provide the sum of the two integer values of the specific variables. Check out the output so that you will know better.

Syntax:

<?php
class pavankumarsake
{
public function add($a1, $b1)
{
return $a1 + $b1;
}
}
class test1
{
public function __construct(pavankumarsake $sum1)
{
echo "the sum of two values 2 and 3 is = ".$sum1->add(2, 3);
}
}
$calc1 = new pavankumarsake();
$test1 = new test1($calc1);
?>
Copy after login

Output:

PHP Type Hinting

Advantages

There are some advantages of using the PHP Type Hinting concept in the PHP Program writing or editin5g. Most of them are listed below. Checkout once.

  • With the help of Type Hinting, one can know the specific expected type of argument of the specific function only at the time of the maintenance.
  • The Function declaration of PHP and its usage would be perfect and obvious after transferring the code to some other developer of PHP language.
  • This makes the PHP code more useful to get some future reference/references.

Conclusion

I hope you learned what is the definition of PHP Type Hinting along with its syntax and explanation, How the Type Hinting works in PHP Programming Language along with various examples to under the PHP TYPE HINTING concept, Advantages of PHP Type Hinting, etc.

The above is the detailed content of PHP Type Hinting. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!