假设我有一个名为的类product
,它有 2 个属性,pid
并且quantity
. 现在,我想创建一个包含此类数组的会话。代码示例:
<?php
public class product{
public $quantity;
public $pid;
public function __construct($pid,$qty){
$this->$pid = $pid;
$this->$quantity = $qty;
}
// some methods ....
}
session_start();
$_SESSION['products'][] = new product(103,10);
$_SESSION['products'][] = new product(87,6);
?>
关于这个的几个问题:
我声明会话的方式是否正确?我没有输入类似
$_SESSION['product'] = new array();
我已经添加对象的东西。当我想阅读 session
product
时,我是否必须将类导入product
页面才能阅读并获得对quantity
和pid
属性的访问权限?代码示例:session_start(); echo $_SESSION['products'][0]->$pid; // should echo "103" (according the the code exmaple above) // OR I will have to do it like this: require_once 'product.inc.php'; session_start(); echo $_SESSION['products'][0]->$pid; // should echo "103"
3.当我访问一个public
属性时,我是否必须像这样访问它:$this->$prop_name
或者$this -> prop_name
(区别在于 $ 符号)