2

Can we use design patterns with Codeigniter? Can we create abstract classes, interfaces and extend them? If I can create those classes, how can I include them? Should those classes reside inside the controller folder or the model folder? If possible can someone explain me, how to implement the following design pattern with Codeignitor? How to include the fills and organize them. Appreciate your support. Thanks!

<?php

interface IAccount {
    public function get_balance();
}

class Savings_account implements IAccount {
    public function get_balance() {
        //get account balance
    }
}

class Current_account implements IAccount {
    public function get_balance() {
        //get account balance
    }
}

class Bank {
    private $acc_type = null;
    public function __construct(IAccount $acc) {
        $this->acc_type = $acc;
    }
    public function get_balance() {
        return $this->acc_type->get_balance();
    }
}

//Client code in a contoller
$bank = new Bank(new Savings_account());
echo $bank->get_balance();
//or
$bank = new Bank(new Current_account());
echo $bank->get_balance();

?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
stackflow
  • 2,112
  • 6
  • 32
  • 45
  • 1
    Design Patterns are framework independent. But their concrete implementation might get complicated with certain frameworks due to the Pattern Languages and Idioms of the framework (or lack thereof). With that said, given that CodeIgniter gets MVC and Active Record wrong already and doesn't follow SOLID principles at all, I'd say you're better off using a different framework. – Gordon Aug 10 '13 at 08:55
  • If you still want to use. in libraries folder you can place your classes and use them as a external libraries. – Adnan Haider Mar 14 '18 at 12:26

0 Answers0