How to: Create and use Factory in Codeigniter4 with example Emman, November 7, 2023November 7, 2023 A factory is a design pattern that allows you to create objects of different types without specifying the exact class name. A factory can have a method that takes a parameter and returns an object of the appropriate type based on the parameter value. This way, you can abstract the creation logic from the client code and make it more flexible and maintainable. CodeIgniter4 supports the use of factories to create and access any class instance from anywhere in your code. You can use the Factories class to access the factory for a specific component, such as models, filters, libraries, etc. You can also define your own custom factories for your own classes. Here is an example of how to use a factory in CodeIgniter4: // Define an interface for the products namespace App\Products; interface ProductInterface { public function getName(); public function getPrice(); } // Implement the interface in different classes namespace App\Products; class Book implements ProductInterface { protected $name; protected $price; public function __construct($name, $price) { $this->name = $name; $this->price = $price; } public function getName() { return $this->name; } public function getPrice() { return $this->price; } } class Pen implements ProductInterface { protected $name; protected $price; public function __construct($name, $price) { $this->name = $name; $this->price = $price; } public function getName() { return $this->name; } public function getPrice() { return $this->price; } } // Define a factory class that creates the products based on a parameter namespace App\Factories; use App\Products\Book; use App\Products\Pen; use CodeIgniter\Config\Factories; class ProductFactory { public static function get($type) { switch ($type) { case 'book': return new Book('CodeIgniter 4', 20); case 'pen': return new Pen('Blue Pen', 1); default: return null; } } } // Use the factory to create and access the products namespace App\Controllers; use App\Factories\ProductFactory; use CodeIgniter\Controller; class ProductController extends Controller { public function index() { // Get a book product $book = ProductFactory::get('book'); echo $book->getName() . ' costs ' . $book->getPrice() . ' dollars.'; // Get a pen product $pen = ProductFactory::get('pen'); echo $pen->getName() . ' costs ' . $pen->getPrice() . ' dollar.'; } } In this example, we have defined an interface for the products and implemented it in two classes: Book and Pen. We have also defined a factory class that has a static method get that takes a parameter and returns an object of the corresponding type. We have used the factory class in the ProductController to create and access the products. For more information on factories, you can refer to the CodeIgniter4 User Guide or the PHP manual. Share this:FacebookX Related Discover more from Code Concepts Snippets Subscribe to get the latest posts sent to your email. Type your email… Subscribe CodeIgniter4 PHP