从另一个类调用php

时间:2013-01-13 02:09:47

标签: php

以下是代码:

<?php 
   class class1{ 
      public function fun1
      { 
         function fun2
         { 
             echo 'Hello';
         } 
      } 
   } 
   class class2{ 
      //calling fun2 
   } 
?>

我可以在 class2 中调用 fun2 功能以及如何使用。

1 个答案:

答案 0 :(得分:2)

是的,只要订单得到正确维护。

class test1
{
    public function doSomething()
    {
        function doSomethingElse()
        {
            echo "doSomethingElse called\n";
        }
    }
}

class test2
{
    public function doSomething()
    {
        doSomethingElse();
    }
}

$t1 = new test1();
$t1->doSomething();

$t2 = new test2();
$t2->doSomething(); // "doSomethingElse called\n";
doSomethingElse(); // "doSomethingElse called\n";

通过调用$t1->doSomething();,加载嵌套的doSomethingElse()函数。如果您没有先调用$t1->doSomething();,那么如果尝试调用嵌套函数,则会收到错误,因为它尚不存在。

//简化回答,see the manual for more details on how/why this is possible

相关问题