0

我有一个问题可能不适合你们大多数人。对不起,如果这对你来说很明显......

这是我的代码:

class Bat
{
      public function test()
      {
        echo"ici";
        exit();
      }

      public function test2()
      {
        $this->test();
      }
}

在我的控制器中:

bat::test2();

我有一个错误:

异常信息:消息:方法“test”不存在并且没有被困在 __call()

4

1 回答 1

1

Bat::test2 指的是静态函数。所以你必须声明它是静态的。

class Bat
{
      public static function test()
      {
        echo"ici";
        exit();
      }

      // You can call me from outside using 'Bar::test2()'
      public static function test2()
      {
        // Call the static function 'test' in our own class
        // $this is not defined as we are not in an instance context, but in a class context
        self::test();
      }
}

Bat::test2();

否则,您需要一个实例Bat并在该实例上调用该函数:

$myBat = new Bat();
$myBat->test2();
于 2012-12-14T13:12:59.460 回答