Add code snippet into a class
<p>I have some function which I use in my cart application:</p>

<pre><code>session_start();

if(!isset($_SESSION['products'], $_SESSION['price'], $_SESSION['product_names'],$_SESSION['product_id'])) {
     $_SESSION['products'] = 0;
     $_SESSION['price'] = 0;
     $_SESSION['product_names'] = array();
     $_SESSION['product_id'] = array();
}
if(isset($_POST['add'])) {
      $_SESSION['product_names'][] = $_POST['product_name']; 
      $_SESSION['products']++;
      $_SESSION['price'] += $_POST['price'];
}
if(isset($_POST['empty'])) {
      session_destroy();
      header('Location:index.php');
}
</code></pre>

<p>The question is, how can I add this into a class and call it in my view page?</p>

<p>I mean it will still work as <code>&lt;?php echo $_SESSION['price']?&gt;</code>. How can I make this work?</p>
