Type Hinting in PHP OOPS

 <?php

//Type Hinting

//Example 1
// function test(array $arr)
// {
//     echo "<table>";
//     foreach($arr as $k => $v)
//     {
//         echo "<tr><td>$k</td><td>$v</td></tr>";
//     }
//     echo "</table>";
// }
// $array = "hkhhlhlh";
// test($array);


//Example 2
interface test
{
    public function doSomething();
}
class ABC implements test
{
    public function doSomething()
    {
        echo "Doing Something from ABC Class";
    }
}
class XYZ implements test
{
    public function doSomething()
    {
        echo "Doing Something from XYZ Class";
    }
    public function doSomethingElse()
    {
        echo "Doing Something Else";
    }
}
function test(ABC $abc)
{
    $abc->doSomething();
}
$abc = new ABC();
test($abc);


Comments

Popular posts from this blog

GROUP BY Clause and HAVING Clause in PHP

Method Overriding in Traits in PHP

Mysqli database Connection and Display Table Data from Database