PHP Classes

File: addModule.example.php

Recommend this page to a friend!
  Classes of Matthew Knowlton   Add Module PHP Trait   addModule.example.php   Download  
File: addModule.example.php
Role: Example script
Content type: text/plain
Description: Example Class Implementing addModule
Class: Add Module PHP Trait
Trait to add pluggable modules to classes
Author: By
Last change:
Date: 8 years ago
Size: 1,464 bytes
 

Contents

Class file image Download
<?php

/* In the Shopping Cart Definition Page */

require_once('addModule.trait.php');

class
shoppingCart{
    use
addModules;
    private
$products;
   
    public function
__construct(){
       
$this->defineModuleType('getProductFromDB', 'single');
       
$this->defineModuleType('checkVoucherCode', 'multiple');
       
       
//initialize shopping cart
   
}
   
    public function
addVoucherDiscountModule($module){
       
$this->addModule('checkVoucherCode', $module);
    }
   
    public function
setProductDataModule($module){
       
$this->addModule('getProductFromDB', $module);
    }
   
    public function
addItem($partNo){
        if(!isset(
$this->products[$partNo])) {
           
$this->products[$partNo] = $this->runModules('getProductFromDB', $partNo)[0];
        }
       
//run add item code
   
}
   
   
//...
   
   
public function checkVoucherCode($voucherCode){
        foreach(
$this->yieldModuleResults($voucherCode) as $results){
            if(
$results['validCode'] != true) continue;
            else {
               
//calculate voucher discount
           
}
        }
    }
}


/* In the Shopping Cart Setup Page */

$cart = new shoppingCart();

$cart->setProductDataModule('getProductFromDatabase');

$cart->addVoucherDiscountModule(function($inputCode){
   
$voucherCodes = getVoucherCodesFromDB();
    foreach(
$voucherCodes as $code){
        if(
$code == strtolower($inputCode)){
            return array(
               
'validCode' => true,
               
'discount' => getVoucherCodeDiscount($code),
            );
        }
    }
    return array(
'validCode' => false);

});