user2737037
user2737037

Reputation: 1157

Get object after outside function call

How can I call functions on the object after a outside class-instance call without using static classes.

Here is my sample (It should echo "OKAY!"):

class class1  {
    function func1()  {
        func3();          // function outside class
    }

    function func2()  {
        echo "AY!";
    }
}

$foo = new class1();
$foo->func1();

function func3()
{
    echo "OK";
    $foo->func2();       // class instance doesn't exist any more
}

Upvotes: 1

Views: 160

Answers (2)

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

Instance pass as argument. follow the code

<?php

class class1  {
    function func1($foo)  {
        func3($foo);          // function outside class
    }

    function func2()  {
        echo "AY!";
    }
}

$foo = new class1();
$foo->func1($foo);

function func3($foo)
{
    echo "OK";
    $foo->func2();       // class instance doesn't exist any more
}
?>

Output:

OKAY!

Upvotes: 0

Mateusz Nowak
Mateusz Nowak

Reputation: 4121

class class1  {
    function func1()  {
        func3($this);          // function outside class
    }

    function func2()  {
        echo "AY!";
    }
}

$foo = new class1();
$foo->func1();

function func3($object)
{
    echo "OK";
    $object->func2();       // class instance doesn't exist any more
}

Upvotes: 4

Related Questions