我的自定义类存在问题:
module/SomeModule/src/SomeModule/Model/someClass.php
我使用 ServiceLocator
获得这样的数据库适配器(完全如 in this Learning Zend Framework 2 tutorial ):
public function getAdapter()
{
if (!$this->adapter) {
$sm = $this->getServiceLocator();
$this->adapter = $sm->get('Zend\Db\Adapter\Adapter');
}
return $this->adapter;
}
在 PHP 5 中它工作得很好,但在 PHP 7 中却不行。看来该类在 PHP 7 中不再是 ServiceLocatorAware
。并给出以下错误:
Fatal error: Uncaught Error: Using $this when not in object context in C:\Zend9\Apache24\htdocs\Project\module\Account\src\Account\Model\User.php:316
Stack trace:
#0 C:\Zend9\Apache24\htdocs\Project\module\Account\src\Account\Model\User.php(271): Account\Model\User::getAdapter()
#1 C:\Zend9\Apache24\htdocs\Project\module\Account\src\Account\Controller\LoginController.php(40): Account\Model\User::userLogin('xxx', 'xxx')
#2 C:\Zend9\ZendServer\data\libraries\Zend_Framework_2\2.4.9\library\Zend\Mvc\Controller\AbstractActionController.php(82): Account\Controller\LoginController->indexAction()
#3 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#4 C:\Zend9\ZendServer\data\libraries\Zend_Framework_2\2.4.9\library\Zend\EventManager\EventManager.php(444): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#5 C:\Zend9\ZendServer\data\libraries\Zend_Framework_2\2.4.9\library\Zend\EventManager\EventManager.php(205): Zend\EventManager\EventManager->trigg
in C:\Zend9\Apache24\htdocs\Project\module\Account\src\Account\Model\User.php on line 316
谁能告诉我为什么 PHP 5 和 PHP 7 之间存在这种差异以及如何解决它?
最佳答案
您或 Zend Framework 正在使用 $this
调用静态成员(类似于使用静态调用来调用非静态成员)。
如果您没有删除一半的错误消息,我可以告诉您具体发生在哪里。
来自http://php.net/manual/en/language.oop5.basic.php
The pseudo-variable
$this
is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object). As of PHP 7.0.0 calling a non-static method statically from an incompatible context results in $this being undefined inside the method. Calling a non-static method statically from an incompatible context has been deprecated as of PHP 5.6.0. As of PHP 7.0.0 calling a non-static method statically has been generally deprecated (even if called from a compatible context). Before PHP 5.6.0 such calls already triggered a strict notice.
关于php - ZF2 使用 PHP5 与 PHP7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40898707/