Let's say I have a class called product
which has 2 properties, pid
and quantity
.
Now, I want to make a session that will hold an array of this class. Code example:
<?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);
?>
Few questions about this:
Is my way of declaring the sessions is correct? I haven't typed something like
$_SESSION['product'] = new array();
I already added objects to it.When I will want to read the session
product
, will I have to import the classproduct
to the page in order to read it and get an access to thequantity
and thepid
properties? Code example: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.when I access a public
property, do I have to access it like this: $this->$prop_name
OR $this -> prop_name
(difference is with the $ sign)