php 脚本尝试执行方法或访问不完整对象的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20664257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The script tried to execute a method or access a property of an incomplete object
提问by Irfan
I'm getting an error, the full error is:
我收到一个错误,完整的错误是:
Fatal error: authnet_cart_process() [<a href='function.authnet-cart-process'>function.authnet-cart-process</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "AuthnetCart" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /home/golfetc/public_html/wp-content/plugins/sccp-2.4.0/authnet_functions.php on line 1266
I'm using session to store cart object in it and get it later at some point. The authnetCart is basically class for cart object.
我正在使用会话将购物车对象存储在其中,并在稍后获取它。authnetCart 基本上是购物车对象的类。
// Check cart in session
if(isset($_SESSION['AUTHNET_CART'])) {
// Get cart from session
$authnetCart = $_SESSION['AUTHNET_CART'];
foreach($authnetCart->getCartItems() as $item) { // Line#1266
if ($item->getItemId() == $subscription_details->ID ) {
$addNewItem = false;
break;
}
}
......
You can see at line 1266, the code doesn't allow me to access its method. Any help will be highly appreciated. Thanks
您可以在第 1266 行看到,该代码不允许我访问其方法。任何帮助将不胜感激。谢谢
回答by Vladimir
You need to include/ requirethe php with your class BEFOREsession_start()like
你需要include/require你的类PHP之前session_start()一样
include PATH_TO_CLASS . 'AuthnetClassFilename.php';
session_start();
if (isset($_SESSION['AUTHNET_CART'])) {
//...
}
回答by Evil Buck
It seems like your answer is in the error message.
您的答案似乎在错误消息中。
Before unserializing AUTHNET_CART, include the class which defines it. Either manually, or using an autoloader.
在反序列化 AUTHNET_CART 之前,包括定义它的类。手动或使用自动加载器。
include PATH_TO_CLASS . 'AuthnetClassFilename.php';
if(isset($_SESSION['AUTHNET_CART'])) {//...
It doesn't appear that you're actually unserializing it either (I'm assuming this was serialized before stuffing it into the session?)
看起来您实际上也没有对其进行反序列化(我假设在将其放入会话之前已对其进行了序列化?)
if(isset($_SESSION['AUTHNET_CART'])) {
// Get cart from session
/** UNSERIALIZE **/
$authnetCart = unserialize($_SESSION['AUTHNET_CART']);
foreach($authnetCart->getCartItems() as $item) { // Line#1266
if ($item->getItemId() == $subscription_details->ID ) {
$addNewItem = false;
break;
}
}
...
回答by CPHPython
None of the other answers in here actually solved this problem for me.
这里的其他答案都没有真正为我解决这个问题。
In this particular case I was using CodeIgniter and adding any of the following lines before the line that caused the error:
在这种特殊情况下,我使用 CodeIgniter 并在导致错误的行之前添加以下任何行:
$this->load->model('Authnet_Class');
OR
或者
get_instance()->load->model('Authnet_Class')
OR
或者
include APPPATH . '/model/Authnet_Class.php';
Did notsolve the problem.
难道没有解决的问题。
I managed to solve it by invoking the class definition in the constructof the class where I was accessing Authnet_Class. I.e.:
我设法通过在我访问的类的构造中调用类定义来解决它Authnet_Class。IE:
class MY_Current_Context_Class extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Authnet_Class');
}
// somewhere below in another function I access Authnet_Class ...
I now understand that the context where you access the Authnet_Classclass, needs to have its definition present on the context's class construct (and not just before you invoke the properties of Authnet_Class).
我现在明白,您访问Authnet_Class类的上下文需要在上下文的类构造中存在其定义(而不仅仅是在调用 的属性之前Authnet_Class)。

