-1

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:

  1. 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.

  2. When I will want to read the session product, will I have to import the class product to the page in order to read it and get an access to the quantity and the pid 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)

kfirba
  • 5,231
  • 14
  • 41
  • 70
  • Rather than attempting to store the whole class instance, why not save the data you're inputting into the class instead? – Jasper Sep 26 '13 at 16:46
  • @Jasper What exactly do you mean? I am storing the data in the class. I can't test it atm since it's a big class which I have to type, and It's a part of a big system. Anyways, your comment isn't helpful at all. – kfirba Sep 26 '13 at 16:55
  • @kfirba Well, to initiate your class, you pass in a couple parameters. My suggestion is to store those parameters in session rather than trying to store the overhead of the class instance. Then whenever you need to use the class, include it and initiate an instance with your stored parameters... Just a suggestion for an alternative method, that may work or not for you, since I don't know what your code does. – Jasper Sep 26 '13 at 17:34

2 Answers2

1
  1. if you define a variable like $var = array(); it will wipe the previous array.

  2. sessions are available throughout your domain/sub-domain, so as long as you have called session_start() before you access the session variable you're covered.

  3. when you say public property are you referring to the class or the method? If you want to access the method inside itself to refer to it like $this->myFunction = $object; if you want to access the method outside itself but inside the class you do it using SELF::$object = new myFunction();

see this: When to use self over $this? and this: http://php.net/manual/en/language.oop5.php

hope this helps.

Community
  • 1
  • 1
Edward
  • 1,806
  • 5
  • 26
  • 36
  • about the second answer, Do I have to import the class to the page in roder to work with it? Or I don't? – kfirba Sep 26 '13 at 17:10
  • It depends, are you talking about your 'product' class? If so yes you will need to call it as it contains the infamous session_start() but i recommend you call session_start() only once in your entire system ie make a new class and function and put it at the top with session_start in it. If you have a login system its a perfect excuse to do it while checking if your user is logged in. The session variable is a superglobal, it is available everywhere, you do not need to parse it in and out of functions and classes. – Edward Sep 26 '13 at 17:18
1
  1. Is my way of declaring the sessions is correct?

Yes. Once you have executed the session_start() the $_SESSION array will exist and be populated with data if any has been saved into previously.

  1. When I want to read the session product, will I have to import the class product to the page in order to read it and get an access to the quantity and the pid properties?

Yes, if you have methods you need to have the class declared in order to use them.

There is a better way of dehydrating and rehydrating a class. That is serialize() and unserialize(). It takes care of all the public, protected and private data, and it also stores the class name. It does not save the methods of course.

Here is some sample code using your original example as a start point. You can run it in your browser to see the results

file product.class.php

<?php
class product{
    public $quantity;
    public $pid;
    private $a = 99;
    protected $b = 88;

    public function __construct($p,$q){
        $this->pid = $p;
        $this->quantity = $q;
    }
    public function show() {
        echo '<pre>SHOW '. $this->pid . '</pre>';
    }
}
?>

file tst99.php

require_once( 'product.class.php' );

session_start();

$p1 = new product(103,10);
$p2 = new product(87,6);

unset( $_SESSION['products'] );

$_SESSION['products'][] = serialize($p1);
$_SESSION['products'][] = serialize($p2);
echo 'Dehydrated class'.PHP_EOL;

echo '<pre>' . print_r( $_SESSION, TRUE ) . '</pre>';
?>

file tst99a.php

<?php
session_start();
require_once('product.class.php');
echo '<pre>' . print_r( $_SESSION, TRUE ) . '</pre>';

echo 'Rehydrated class'.PHP_EOL;
$np1 = unserialize($_SESSION['products'][0]);
$np2 = unserialize($_SESSION['products'][1]);

echo '<pre>' . print_r( $np1, TRUE ) . '</pre>';
echo '<pre>' . print_r( $np2, TRUE ) . '</pre>';
$np1->show();
$np2->show();

Run tst99.php first and then tst99a.php to see that the class gets fully rehydrated.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149