Type Hinting pada PHP

Secara sederhana Type Hinting beratri memberi petujuk yang hanya berfungsi untuk hanya menerima type data yang dikirimkan. Dalam kata lain Type Hinting adalah metode yang dapat memaksa method / function untuk menerima tipe data yang diberikan. Pada PHP kita bisa menggunakan Type Hinting pada sebuah Object, Array dan tipe data Callable. Callable Type Hinting hanya tersedia dari PHP versi 5.4. Berikut contoh type hinting pada PHP :
<?php
//Class where we are going to implement typehinting
class Typehinting_Test
{
//Implementation Type Hinting in PHP
//Forcing to pass argument as object of class Test1
public function type_hint_method(Test1 $parameter)
{
$parameter->test_method();
}
}
//Below is class
class Test1
{
public function __construct()
{
//Do Nothing
}
//Method to call in the type hinting class
public function test_method()
{
echo 'Type Hinting in PHP works';
}
}
$th_test = new Typehinting_Test();
//$th_test->type_hint_method(1221) //Will give fatal error: Catchable fatal error: Argument 1 passed to Typehinting_Test::type_hint_method() must be an instance of Test1, integer given
$t1 = new Test1();
$th_test->type_hint_method($t1); // Print Type Hinting in PHP works
?>

Related Posts :

Footer Right