【问题标题】:Php object array (get() method not working)PHP 对象数组(get() 方法不起作用)
【发布时间】:2021-09-04 02:56:06
【问题描述】:

我正在尝试创建一个对象数组,但有几个问题。

  • 首先我不能在 ItemModel 类函数中使用 $item= array() 而不使其成为全局函数。
  • 其次,我得到了一些奇怪值的数组。

我是编程新手,谁能解释一下我做错了什么?

class ItemModel{
           
        private $item= array();
                    
        public function setItems(){

            global $item;
            $test = new Bmw("test....", "BMW", 32, 1, 120);
            $item[] = $test;

        }

        public function getItems(){
            global $item;
            return $item;

        }
            
           
 }

abstract class Car{

    private $id;
    private $model;
    private $price;
    private $carTypeId;

    public function __construct($id, $model, $price, $carTypeId){

        $this->$id= $id;
        $this->$model= $model;
        $this->$price = $price;
        $this->$carTypeId = $carTypeId;
        
    }

    public abstract function getAdditionalInfo();

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

}

class Bmw extends Car{

    
    private $weight;

    public function __construct($id, $model, $price, $carTypeId, $weight) {
        
        parent::__construct($id, $model, $price, $carTypeId);
        $this->$weight= $weight;
    }

    public function getAdditionalInfo(){

        return "Weight: ".$this->$weight;
        
    }

    
}


class ItemView extends ItemModel{
    

    public function showItems(){
        
        $this->setItems();
       
       
        foreach ($this->getItems() as $item) {
            
            print_r($item);
            
        }
        die;    

        

    }

}
$test = new ItemView();
$test->showItems();

Results: 
Bmw Object
    (
        [weight:Bmw:private] => 
        [id:Car:private] => 
        [model:Car:private] => 
        [price:Car:private] => 
        [carTypeId:Car:private] => 
        [test....] => test....
        [BMW] => BMW
        [32] => 32
        [1] => 1
        [120] => 120
    )

当我尝试通过更改来使用函数 getId() 时

foreach ($this->getItems() as $item) {
                
                print_r($item->getId());
                
            }

我明白了

PHP Warning:  Undefined variable $id in /workspace/Main.php on line 43
PHP Warning:  Undefined property: Bmw::$ in /workspace/Main.php on line 43

【问题讨论】:

  • $this->item 应该可以工作
  • 在引用例如时从所有类属性中删除 $ 符号$this->$id= $id; => $this->id= $id;
  • 感谢 Alberto Sinigaglia 和 B001ᛦ。通过删除 $ 修复了该问题

标签: php arrays class object constructor


【解决方案1】:

我确实尝试重构您的代码并添加一些 cmets 我更改了什么以及为什么。 你已经做了一些很棒的工作。我认为你的问题是使用global 而不是真正了解 php 类的语法。

<?php

// I did remove the ItemModel Class because it looks like this should be your main code
// see at the end how we add an item from the main code

abstract class Car {

    private $id;
    private $model;
    private $price;
    private $carTypeId;

    public function __construct($id, $model, $price, $carTypeId){
        // when u want to access properties, you dont need specify the property with the dollar sign
        $this->id = $id;
        $this->model = $model;
        $this->price = $price;
        $this->carTypeId = $carTypeId;

    }

    public abstract function getAdditionalInfo();

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

}

class Bmw extends Car {

    private $weight;

    public function __construct($id, $model, $price, $carTypeId, $weight) {

        parent::__construct($id, $model, $price, $carTypeId);
        $this->weight = $weight;
    }

    public function getAdditionalInfo(){

        return "Weight: ".$this->weight;

    }


}

// your ItemView class ist just a handler to store multiple items and get them back
// you also dont need to use global $item here, because you want to access the property of the current object
// when you have multiple ItemView objects it would really bug "hungry" :)
class ItemView {
    private $items = array();

    public function addItem($item){
        $this->items[] = $item;

    }

    public function getItems(){
        return $this->items;
    }
}

// at the end it is just your items been stored in ItemView and then later getting accessed
$test = new ItemView();
$test->addItem(new Bmw("test....", "BMW", 32, 1, 120));

foreach($test->getItems() as $item) {
    print_r($item);
}

// die for whatever reason
die();

【讨论】:

    【解决方案2】:

    你可以找到并运行代码here

    <?php
        class ItemModel{
                   
                private $item = array();
                            
                public function setItems($item){
                    $this->item[] = $item;
                }
        
                public function getItems(){
                    return $this->item;
                }
                
                public function getItemsAsArray(){
                    $arrayitem = array();
                    $arrayitems = array();
                    foreach ($this->item as $item) {
                        $arrayitem['ID'] = $item->getId();
                        $arrayitem['Model'] = $item->getModel();
                        $arrayitem['Price'] = $item->getPrice();
                        $arrayitem['TypeID'] = $item->getcarTypeId();
                        $arrayitem['Info'] = $item->getAdditionalInfo();
                        $arrayitems[] = $arrayitem;
                    }
                    return $arrayitems;
                }
         }
        
        abstract class Car{
        
            private $id;
            private $model;
            private $price;
            private $carTypeId;
        
            public function __construct($id, $model, $price, $carTypeId){
                $this->id = $id;
                $this->model = $model;
                $this->price = $price;
                $this->carTypeId = $carTypeId;
            }
        
            public abstract function getAdditionalInfo();
        
            public function getId(){
                return $this->id;
            }
            public function getModel(){
                return $this->model;
            }
            public function getPrice(){
                return $this->price;
            }
            public function getcarTypeId(){
                return $this->carTypeId;
            }    
        }
        
        class Bmw extends Car{
        
            private $weight;
        
            public function __construct($id, $model, $price, $carTypeId, $weight) {
                
                parent::__construct($id, $model, $price, $carTypeId);
                $this->weight = $weight;
            }
        
            public function getAdditionalInfo(){
                return $this->weight;
            }
        }
        
        
        class ItemView extends ItemModel{
            
        
            public function showItems(){
                $this->setItems(new Bmw("test....", "BMW", 32, 1, 120));
                $this->setItems(new Bmw("another....", "Z6", 16, 2, 10));
               
                 print_r($this->getItems());  //return BMW Object
                //  you can use somthing like this with BMW Object
                 print_r($this->getItems()[0]->getAdditionalInfo()."\n");
                 //or use my function
                 print_r($this->getItemsAsArray());
                 // or
                 foreach($this->getItemsAsArray() as $getItemsAsArray) {
                    foreach($getItemsAsArray as $key => $value) {
                        print_r($key."=".$value."\n");
                     }
                 }
            }
        
        }
        $test = new ItemView();
        $test->showItems();
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 2017-09-11
      • 2017-05-16
      相关资源
      最近更新 更多