不能使用__PHP_Incomplete_Class类型的对象作为数组(Session Related?)

时间:2017-01-10 03:13:19

标签: php oop session

我有3个单独的文件,item.phpproposal.phpoutput.php - 这应该是一个类似于应用的购物车,其目的是让用户选择一个项目,将项目输入Proposal类...但是我遇到了以下错误:

  

致命错误:未捕获错误:无法在C:\ xampp \ htdocs \ proposal.php中使用__PHP_Incomplete_Class类型的对象作为数组:12堆栈跟踪:#0 C:\ xampp \ htdocs \ output .php(9):在第12行的C:\ xampp \ htdocs \ proposal.php中抛出的Proposal-> addItem(Object(Item))#1 {main}

我搜索了SO&谷歌,并尝试了各种各样的事情,包括在session_start()item.php的包含之前放置proposal.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;
        }                                
    }

proposal.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
                )

        )

) 

1 个答案:

答案 0 :(得分:3)

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

您的会话在声明类之前已初始化。

这导致__PHP_Incomplete_Class。

问题“不能使用Proposal类型的对象作为数组”:

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

如果您的会话包含密钥提议,那么您将其用作存储变量,但这会在output.php中初始化为Proposal的实例:

$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'];
    }
}