Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将类型为_PHP_complete_类的对象用作数组(会话相关?)_Php_Oop_Session - Fatal编程技术网

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

无法将类型为_PHP_complete_类的对象用作数组(会话相关?),php,oop,session,Php,Oop,Session,我有3个独立的文件,item.php,proposal.php和output.php-这应该是一个类似购物车的应用程序,其目的是让用户选择一个项目,并将项目放入proposal类中。。。但是,我遇到了以下错误: 致命错误:未捕获错误:无法在C:\xampp\htdocs\proposal.PHP中使用类型为uu PHP\u complete\u类的对象作为数组。PHP:12堆栈跟踪:#0 C:\xampp\htdocs\output.PHP(9):proposal->addItem(object

我有3个独立的文件,
item.php
proposal.php
output.php
-这应该是一个类似购物车的应用程序,其目的是让用户选择一个项目,并将项目放入
proposal
类中。。。但是,我遇到了以下错误:

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

我搜索了SO&Google,尝试了各种方法,包括将
session\u start()
放在
item.php
proposal.php
的includes之前,但这并没有解决问题,错误只是变为:

无法将建议类型的对象用作数组

有什么想法吗?运行PHP7.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;
        }                                
    }
    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;
            }
        }
    }
    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>'; }
output.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;
        }                                
    }
    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;
            }
        }
    }
    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>'; }
在声明类之前,会话已初始化

这导致了uu PHP_不完整的u类

“无法将建议类型的对象用作数组”的问题:

如果会话包含键
建议
,则将其用作存储变量,但在
output.php中将其初始化为
建议
的实例:

class Proposal {
    protected function __construct() {}

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

        return $_SESSION['proposal'];
    }
}

避免这种情况的一种方法是创建一个单独的提案会话:


这确实解决了我在上述两个错误中遇到的问题,但是生成了另一个错误“无法将Item类型的对象用作数组”。我发现这个新错误是由以下行引起的:
$this->items[$id]['qty']=$this->items[$id]['qty']+$item->getQty()我通过将代码更改为
$this->items[$id]->qty=$this->items[$id]->qty+$item->getQty()来更正此问题存在的问题是,我试图以数组的形式不正确地访问item对象。谢谢你的回答!
class Proposal {
    protected function __construct() {}

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

        return $_SESSION['proposal'];
    }
}