Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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 从数组对象获取数据_Php - Fatal编程技术网

Php 从数组对象获取数据

Php 从数组对象获取数据,php,Php,嗨,我是PHP新手,有以下问题。我已经写了下面的代码将数据添加到数组中,现在我需要查看添加的数据,请告诉我如何做 class ShoppingCart { private $items = array(); private $n_items = 0; function addItem( Item $item ) { $this->items[] = $item; $this->n_items = $this->n_items + 1; //print_r (array_val

嗨,我是PHP新手,有以下问题。我已经写了下面的代码将数据添加到数组中,现在我需要查看添加的数据,请告诉我如何做

class ShoppingCart
{
private $items = array();
private $n_items = 0;
function addItem( Item $item )
{
 $this->items[] = $item;
$this->n_items = $this->n_items + 1;
//print_r (array_values($this->items));
echo "item $this->items added sussesfully";
}
}


谢谢

因为
$items
是私有属性,您需要在
ShoppingCart
类上创建一个新方法

public function getItems()
{
    return $this->items;
}
然后通过调用新方法检索
$items
数组

$objectname = new ShoppingCart();
$items = $objectname->getItems();

var_dump($items);
“查看添加的数据”是什么意思?
public function getItems()
{
    return $this->items;
}
$objectname = new ShoppingCart();
$items = $objectname->getItems();

var_dump($items);