2017-01-10 10 views
0

を、私は3つの別々のファイル、item.phpproposal.phpoutput.phpを持っている - これは、アプリケーションのように、カートことになって、アイデアが項目を選択するユーザーのためにあると私はSO & Googleの周りに検索したは(セッション関連?)の配列としてタイプ__PHP_Incomplete_Classのオブジェクトを使用することはできません

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

、およびitem.phpproposal.phpのために含まれる前にsession_start()を配置するなど、いろいろなことを試してみました:Proposalクラスへの項目...しかし、私は次のエラーに実行しています、しかし、それは問題を解決していない、エラーはちょうどに変更されました:

Cannot use object of type Proposal as array

どのようなアイデアですか?

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 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; 
      } 
     } 
    } 

を実行します0

EDIT: 2回目の実行までエラーが表示されないため、この問題はセッション関連の可能性があります。最初の実行上の

出力は次のとおりです。

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
session_start(); 
include('item.php'); 
include('proposal.php'); 

クラスが宣言されている前に、あなたのセッションが初期化されます。

これは、__PHP_Incomplete_Classにつながっています。 「配列として型提案のオブジェクトを使用することはできません」と

問題:

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

セッション鍵の提案が含まれている場合は、ストレージ変数としてそれを使用しているが、これは、出力での提案のインスタンスとして初期化されます。 PHP:

:これを回避する

$proposal = new Proposal(); 

$proposal->addItem($item); 

$_SESSION['proposal'] = $proposal; 

一つの方法は、提案のセッションシングルトンを作成することです3210

class Proposal { 
    protected function __construct() {} 

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

     return $_SESSION['proposal']; 
    } 
} 
+0

これは上記の2つのエラーのために私の問題を解決しましたが、もう1つのエラーが発生しました。 "タイプItemのオブジェクトを配列として使用できません"。私は、この新しいエラーが次の行によるものだと分かりました: '$ this-> items [$ id] ['qty'] = $ this-> items [$ id] ['qty'] + $ item-> getQty(); 'コードを$ this-> items [$ id] - > qty = $ this-> items [$ id] - > qty + $ item-> getQty();に変更してこれを修正しました。問題は、配列のようにアイテムオブジェクトに不正にアクセスしようとしていたことです。ご回答有難うございます! – gb2016

関連する問題