1

在不使用静态类的情况下,如何在外部类实例调用之后调用对象上的函数。

这是我的示例(它应该回显“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
}
4

2 回答 2

4
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
}
于 2013-09-07T13:23:15.927 回答
0

实例作为参数传递。按照代码

<?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
}
?>

输出:

OKAY!
于 2013-09-07T13:27:11.703 回答