Zend Framework 2:致命错误 - 在不在对象上下文中时使用 $this
Zend Framework 2: Fatal error - using $this when not in object context
这是我的代码,在 Module.php:
public function onBootstrap(MvcEvent $mvcEvent)
{
$sm = $mvcEvent->getApplication()->getServiceManager();
$myServiceEM = $sm->get('MyModule\Service\MyService')->getEventManager();
$myServiceEM->attach(
'myevent.post', ['MyModule\Controller\MyController', 'myFunction']
);
}
这里是 myFunction()
MyModule\Controller\MyController:
public function myFunction(Event $e)
{
$myTestFunc = $this->getMyTestFunc();
}
但是当我调用getMyTestFunc()
函数时出现这个错误:
Fatal error: Uncaught Error: Using $this when not in object context
['MyModule\Controller\MyController', 'myFunction']
是一个callable for a static method;因此你不能在那里使用 $this
。
考虑
<?php
class Foo {
protected $prop = 'value';
public function memberMethod() {
return $this->prop;
}
public static function staticMethod() {
return 'static';
}
}
echo Foo::staticMethod(), "\r\n";
// Foo::memberMethod(); // results in Fatal error: Using $this when not in object context
bar( 'myevent.post', array('Foo', 'staticMethod') );
// bar( 'myevent.post', array('Foo', 'memberMethod') ); // // again: Fatal error: Using $this when not in object context
// to call memberFunction() you need an instance to call the method on
$foo = new Foo;
echo $foo->memberMethod(), "\r\n";
bar( 'myevent.post', array($foo, 'memberMethod') );
function bar( $something, $callback ) {
// invoking the callback immediately
// ZendFramework will store the callback and invoke it when the specified event occurs
echo $callback(), "\r\n";
}
现在将 function bar()
替换为 Zend\EventManager::attach()
,将 class Foo
替换为 MyModule\Controller\MyController
,问题就出在 "at" 上。
这是我的代码,在 Module.php:
public function onBootstrap(MvcEvent $mvcEvent)
{
$sm = $mvcEvent->getApplication()->getServiceManager();
$myServiceEM = $sm->get('MyModule\Service\MyService')->getEventManager();
$myServiceEM->attach(
'myevent.post', ['MyModule\Controller\MyController', 'myFunction']
);
}
这里是 myFunction()
MyModule\Controller\MyController:
public function myFunction(Event $e)
{
$myTestFunc = $this->getMyTestFunc();
}
但是当我调用getMyTestFunc()
函数时出现这个错误:
Fatal error: Uncaught Error: Using $this when not in object context
['MyModule\Controller\MyController', 'myFunction']
是一个callable for a static method;因此你不能在那里使用 $this
。
考虑
<?php
class Foo {
protected $prop = 'value';
public function memberMethod() {
return $this->prop;
}
public static function staticMethod() {
return 'static';
}
}
echo Foo::staticMethod(), "\r\n";
// Foo::memberMethod(); // results in Fatal error: Using $this when not in object context
bar( 'myevent.post', array('Foo', 'staticMethod') );
// bar( 'myevent.post', array('Foo', 'memberMethod') ); // // again: Fatal error: Using $this when not in object context
// to call memberFunction() you need an instance to call the method on
$foo = new Foo;
echo $foo->memberMethod(), "\r\n";
bar( 'myevent.post', array($foo, 'memberMethod') );
function bar( $something, $callback ) {
// invoking the callback immediately
// ZendFramework will store the callback and invoke it when the specified event occurs
echo $callback(), "\r\n";
}
现在将 function bar()
替换为 Zend\EventManager::attach()
,将 class Foo
替换为 MyModule\Controller\MyController
,问题就出在 "at" 上。