php - Using cart class with sessions -


i've started learn php classes, interfaces, extends , things related those.

before i've worked functions , started quite mess hundreads of separate functions.

there multiple classes project based on needs:

  1. cart class. should quite generic because @ point might need save data database instead of sessions.
  2. calculations class. there around 15 products , around 5 different type of calculations. (based on amount, base price, price per person etc.)
  3. database class. used prices database calculations class.
  4. create pdf mpdf submitted data through html form (which used update cart amounts , return calculated prices in real time)

however, give hint on i'm trying achieve, might give better understanding on how should approach things.

now actual questions. here's cart class:

interface cartinterface {     public function additem($id, $amount, $months);     public function updateitem($id, $amount, $months);     public function deleteitem($id);     public function countitems();     public function getcart();     public function ifidexists($id);     }  class cart implements cartinterface { public $cart = array();  public function additem($id, $amount, $months) {      if($this->ifidexists($id)){         $this->updateitem($id, $amount, $months);                                        }      else {         $this->cart[$id]['id'] = $id;         $this->cart[$id]['amount'] = $amount;          $this->cart[$id]['months'] = $months;     }  }  public function updateitem($id, $amount, $months) {         $this->cart[$id]['id'] = $id;         $this->cart[$id]['amount'] = $amount;          $this->cart[$id]['months'] = $months;    }  public function deleteitem($id) {     unset($this->cart[$id]);         }  public function countitems() {     return count($this->cart); }  public function getcart() {     return $this->cart; }  public function ifidexists($id) {     $exists = false;     if(array_key_exists($id, $this->cart)) {         $exists = true;     }     else {         $exists = false;     }     return $exists; }  public function showcart() {     foreach($this->cart $value) {         echo "<br />";         echo 'id:'. $value['id'] ;         echo "<br />";         echo 'amount:'.$value['amount'];         echo "<br />";                   echo 'months:'.$value['months'];         echo "<br />";     } } } 

that has pretty functions cart need here questions:

  1. my html form has input field each item, determinates amount of corresponding item ordered. need separate additem() , updateitem() in case? because newer value anyway rewrite older.

  2. what determinates on methods should include in cartinterface? know class using cartinterface forced use methods reason want force class use methods?

  3. the important question adapting class use sessions. know, cart isn't of use without sessions. how can use $cart->deleteitem() delete item session? need create separate sessioncart class, has same methods cart deals sessions?

    $cart = new cart(); $cart->additem('1', '30', '2'); $cart->additem('2', '306', '12'); $cart->additem('6', '306', '12'); $cart->deleteitem('1');  // here create session array cart cart's class methods won't work $cart = $cart->getcart();  $_session['cart'] = $cart; $cart->deleteitem('1'); // won't effect session cart print_r($_session['cart']);  

i appreciate , please point out bad coding habits, reading post learn something.

long story short: approach should use cart class work sessions?

  1. your implementations of adding , item , updating 1 identical. relevant know whether items being added first time or being replaced? if answer yes, should keep 2 separate methods. there people tell you're violating dry rule (don't repeat yourself), rule applies semantically. things write same don't mean same allowed repetition.

  2. interfaces way ensuring contract met. imagine have class able generate invoice cart. , imagine implement cart freebiecart, gives random item cart free. invoicer class this:

    class invoicer {      /**     * @return invoice     */     public function invoicefromcart(cartinterface $cart) {         //in here know expect $cart object         $cart->getitems();         $cart->countitems();     } ... } 

    as can see, providing interface convenient code going using class, because doesn't need know concrete implementation of cartinterface receiving, needs know contract call methods , communicate it. allows interchange functionality without having modify big amounts of code.

    to answer comment, it'd weird make sense of class invoicer extending cart in oop. meaning of keyword "extends" class extending more specialized version of more abstract class. think on car extends vehicle or earth extends planet.

  3. there couple of ways can go this. important thing me wrap session own abstraction. cartstorage. class implement interface this:

    interface cartstorage {     public function store(cartinterface $cart);     public function retrieve(sessionid $sessionid); } 

    then you'd need implement sessioncartstorage class stores , allows retrieve carts session. if later on you'd store carts in database or somewhere else, able implement postgrescartstorage or cassandracartstorage have means of storing cart in storage systems without need of changing code.

    to answer comment, it'd depend on way , level of granularity want when storing cart in session. if store item item, indeed have method sessioncartstorage->additem(cartitem $item). complicate way of retrieving cart session, keep simple storing whole cart instead.

hope helps.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -