无法将__PHP_Incomplete_Class类型的对象用作数组(会话相关?)

zpf6vheq  于 11个月前  发布在  PHP
关注(0)|答案(2)|浏览(60)

我有3个单独的文件,item.phpproposal.phpoutput.php-这应该是一个购物车一样的应用程序,其想法是为用户选择一个项目和项目的Proposal类...但是我遇到了以下错误:

**致命错误:Uncaught Error:**Cannot use object of type __PHP_Incomplete_Class as array in C:\xampp\htdocs\proposal.php:12堆栈跟踪:#0 C:\xampp\htdocs\output.php(9):Proposal->addItem(Object(Item))#1 {main} thrown in C:\xampp\htdocs\proposal.php on line 12

我已经在SO & Google上搜索过了,尝试了各种方法,包括将session_start()放在item.phpproposal.php的包含之前,但是这并没有解决这个问题,错误只是变成了:
不能将Proposal类型的对象用作数组
运行PHP 7.0.9

item.php

<?php

    class Item {
        protected $id;
        protected $name;
        protected $manufacturer;
        protected $model;
        protected $qty;
        protected $serial;

        public function __construct($id,$name,$manufacturer,$model,$qty,$serial) {
            $this->id = $id;
            $this->name = $name;
            $this->manufacturer = $manufacturer;
            $this->model = $model;
            $this->qty = $qty;
            $this->serial = $serial;
        }

        public function getId() {
            return $this->id;
        }

        public function getName() {
            return $this->name;
        }

        public function getManufacturer() {
            return $this->manufacturer;
        }

        public function getModel() {
            return $this->model;
        }

        public function getQty() {
            return $this->qty;
        }

        public function getSerial() {
            return $this->serial;
        }                                
    }

字符串

提案.php

class Proposal {
        protected $items = array();

        public function __construct() {
            $this->items = isset($_SESSION['proposal']) ? $_SESSION['proposal'] : array();
        }

        public function addItem(Item $item) {
            $id = $item->getId();
            // the following line is line 12 of proposal.php
            if(isset($this->items[$id])) {
                $this->items[$id]['qty'] = $this->items[$id]['qty'] + $item->getQty();
            }
            else {
                $this->items[$id] = $item;
            }
        }
    }

output.php

session_start();
    include('item.php');
    include('proposal.php');

    $item = new Item($_GET['id'],$_GET['name'],$_GET['manufacturer'],$_GET['model'],$_GET['qty'],$_GET['serial']);
    $proposal = new Proposal();

    $proposal->addItem($item);

    $_SESSION['proposal'] = $proposal;

    // view output in array/object format if session variable set
    if(isset($_SESSION['proposal'])) { print '<pre>' . print_r($_SESSION['proposal'],1) . '</pre>'; }

**编辑:**我相信这个问题可能与会话有关,因为错误直到第二次运行才出现。

第一次运行的输出为:

Proposal Object
(
    [items:protected] => Array
        (
            [25] => Item Object
                (
                    [id:protected] => 25
                    [name:protected] => Computer
                    [manufacturer:protected] => Dell
                    [model:protected] => Alienware
                    [qty:protected] => 11
                    [serial:protected] => 12345678
                )

        )

)

cig3rfwq

cig3rfwq1#

session_start();
include('item.php');
include('proposal.php');

字符串
在声明类之前初始化会话。
这将导致__PHP_Incomplete_Class。
“无法将Proposal类型的对象用作数组”的问题:

public function __construct() {
        $this->items = isset($_SESSION['proposal']) ? $_SESSION['proposal'] : array();
    }


如果你的session包含key proposal,你将它用作存储变量,但它被初始化为output.phpProposal的示例:

$proposal = new Proposal();

$proposal->addItem($item);

$_SESSION['proposal'] = $proposal;


避免这种情况的一种方法是创建Proposal的会话单例:

class Proposal {
    protected function __construct() {}

    public static function getInstance()
    {
        if (!isset($_SESSION['proposal'])) {
            $_SESSION['proposal'] = new Proposal;
        }

        return $_SESSION['proposal'];
    }
}

nwsw7zdq

nwsw7zdq2#

如果要将对象保存到会话变量,请执行以下过程:
在PHP中,在存储对象之前使用serialize(),在从session中检索对象时调用unserialize()。

存储对象

session_start();
 $object = new sample_object();
 $_SESSION['sample'] = serialize($object);

字符串

检索对象

session_start();
$object = unserialize($_SESSION['sample']);

相关问题