gb2016
gb2016

Reputation: 111

Cannot use object of type __PHP_Incomplete_Class as array (Session Related?)

I have 3 separate files, item.php, proposal.php and output.php - this is supposed to be a cart like application, the idea is to for the user to select an item and the item in to the Proposal class... however I am running in to the following error:

Fatal error: Uncaught Error: Cannot use object of type __PHP_Incomplete_Class as array in C:\xampp\htdocs\proposal.php:12 Stack trace: #0 C:\xampp\htdocs\output.php(9): Proposal->addItem(Object(Item)) #1 {main} thrown in C:\xampp\htdocs\proposal.php on line 12

I've searched around SO & Google, and tried various things including placing session_start() before the includes for item.php and proposal.php, however that did not solve the issue, the error just changed to:

Cannot use object of type Proposal as array

Any ideas? Running 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>'; }

EDIT: I believe this issue may be session related because the error does not appear until the 2nd run.

Output on first run is:

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
                )

        )

) 

Upvotes: 3

Views: 7192

Answers (2)

Rajon Kobir
Rajon Kobir

Reputation: 173

If you want to save an object to a session variable, then follow these processes :

Use serialize() in PHP before store your object, and call unserialize() when retrieve your object from session.

store object

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

retrieve object

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

Upvotes: 0

Niko Hujanen
Niko Hujanen

Reputation: 733

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

Your session is initialized before classes have been declared.

This is leading to __PHP_Incomplete_Class.

Problem with "Cannot use object of type Proposal as array":

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

If your session contains key proposal, you are using it as storage variable, but this is initialized as instance of Proposal in output.php:

$proposal = new Proposal();

$proposal->addItem($item);

$_SESSION['proposal'] = $proposal;

One way to avoid this is to create a session singleton of Proposal:

class Proposal {
    protected function __construct() {}

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

        return $_SESSION['proposal'];
    }
}

Upvotes: 4

Related Questions