Q:

PHP/laravel Adding item to session not saved

yesterday i started my first laravel project. but i'm having a problem i can't understand. 
I'm trying to create a shopping cart, which i keep track of using laravel's session object with my own wrapper. the code looks like this:
class SessionController extends Controller
{
 static function getSessionData($key = null, $data = null)
    {
        if($data === null)
        {
            return Session::get($key);
        }

        else
        {
            return Session::get($key, $data);
        }
    }

    static function allSessionData()
    {
        return Session::all();
    }

    static function putSessionData($key = null, $data)
    {
        if ($key === null)
        {
            Session::put($data);
        }
        else
        {
            Session::put($key, $data);
        }
    }

    static function has($key)
    {
        return Session::has($key);
    }  

Now inside my ShoppingCart class i created an $instance field, which keeps the shoppingcart data, or creates an empty shopping cart if it's not in the session yet. the code looks like this: 
class ShoppingCart extends Model
{
    public $id;
    public $session;
    private $sessionName = 'shoppingcart';
    private $instance;
    public $cartItems;

    protected $connection = 'mysql2';
    protected $table = 'shoppingcart';
    protected $primaryKey = 'Id';   

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->cartItems = [];
    }

    private function getOrCreateSession()
    {
        if(SessionController::has($this->sessionName))
        {
            $this->instance = SessionController::getSessionData($this->sessionName);
        }

        else
        {
            SessionController::putSessionData($this->sessionName, $this);
            $this->instance = SessionController::getSessionData($this->sessionName);
        }
    }

    function addCartItem($productId, $qty = 1)
    {
        $this->getOrCreateSession();
        $cartItem = $this->createCartitem($productId, $qty);
        $content = $this->getContent();

        if ($existingCartitem = $this->alreadyInCart($cartItem)) {
            $existingCartitem->qty += $cartItem->qty;
        } else {
            array_push($content, $cartItem);
        }
    }

    function createCartItem($productId, $qty)
    {
        $cartItem = CartItem::fromId($this->instance->id, $productId, $qty);
        $cartItem->associate($this->instance->id);

        return $cartItem;
    }

    private function alreadyInCart($cartItem)
    {
        $alreadyInCart = FALSE;

        foreach ($this->instance->cartItems as $item) {
            if ($item->productId == $cartItem->productId) {
                return $item;
            }
        }

        return $alreadyInCart;
    }

    //returns current shoppingcart contents.
    private function getContent()
    {
       return $this->instance->cartItems;
    }
}

Now inside the addCartItem-method i try to push the new cartitem into the array using array_push()but var_dump()-ing the session afterwards shows the session does not contain the newly added item. However, if i var_dump() the $content before and after the array_push-method, i can see it's added.
I thought PHP would pass the session by reference but apparantly i'm missing something here. 
What am i doing wrong? 
Thank you in advance. 

A:

You will need to return the session by reference, i.e.
static function &getSessionData($key = null, $data = null)

and use it by getting its reference, like:
$myVariable = &SessionController::getSessionData($this->sessionName);

Quote from the docs:

Returning by reference is useful when you want to use a function to
  find to which variable a reference should be bound. Do not use
  return-by-reference to increase performance. The engine will
  automatically optimize this on its own. Only return references when
  you have a valid technical reason to do so. To return references, use
  this syntax:

<?php
class foo {
    public $value = 42;

    public function &getValue() {
        return $this->value;
    }
}

$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue;                // prints the new value of $obj->value, i.e. 2.
?>

