PHP programs to demonstrate the function returning object

bookmark

<?php
//PHP programs to demonstrate the function returning object.
class Number
{
    public $Num;

    function __construct($n)
    {
        $this->Num = $n;
    }

    function Display()
    {
        echo "Num: " . $this->Num;
    }
}

function AddObj($obj1, $obj2)
{
    $temp = new Number(0);

    $temp->Num = $obj1->Num + $obj2->Num;

    return $temp;
}

$Obj1 = new Number(10);
$Obj2 = new Number(20);

$Obj3 = AddObj($Obj1, $Obj2);

$Obj3->Display();

?>


Output
Num: 30