From the course: PHP: Design Patterns
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
The factory pattern in action - PHP Tutorial
From the course: PHP: Design Patterns
The factory pattern in action
- In this case, we're going to use Zend\Config to show how the factory pattern works in real life. Zend\Config is already installed in our Vendor folder, via Composer here. We specified Zend\Config, and it automatically installed in the Vendor folder for us. In this case, to load as factory config, we pass a file name to the Zend\Config\Factory. Behind the scenes, it uses a file type-specific parser to read the file accordingly. In our first example here, we used config.json. Behind the scenes, it uses the config factory to load config.json and load all these values in the memory. In the second case, we used config.xml. It also uses a file-based parser to load all of these values in the memory. If we wanted to, we could use YAML, PHP, or even a simple INI file. We can switch types and even add our own by adding additional class implementations.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.