Home arrow PHP arrow Page 2 - Removing Singletons in PHP

Removing Singletons in PHP

In this conclusion to a two-part tutorial, I demonstrate through a fairly realistic example that the elimination of Singletons (and their associated side effects, of course) is a pretty straightforward process. Indeed, I show that it can be successfully tackled using a proper mixture of different programming methodologies and guidelines, such as Inversion of Control and the Separation of Concerns Principle.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
December 08, 2011
  1. · Removing Singletons in PHP
  2. · Fetching User Entities from Storage in PHP

print this article

Removing Singletons in PHP - Fetching User Entities from Storage in PHP
(Page 2 of 2 )

As I promised in the segment that you just read, below I coded a simple script. It uses the refactored classes that you saw before to fetch a few user entities from the storage layer. Have a look at it:


use Singletons\Library\Loader\Autoloader as Autoloader,
    Singletons\Injector\MysqlAdapterFactory as MysqlAdapterFactory,
    Singletons\Model\Mapper\UserMapper as UserMapper;

// include the autoloader
require_once __DIR__ . '/Library/Loader/Autoloader.php';
$autoloader = new Autoloader;

// create an instance of the MySQL adapter via the low-level factory
$adapterFactory = new MysqlAdapterFactory;
$mysqlAdapter = $adapterFactory->create();

// create an instance of the user mapper
$userMapper = new UserMapper($mysqlAdapter);

// fetch all users from the storage
$users = $userMapper->find();
foreach ($users as $user) {
    echo ' ID: ' . $user->id
         . ' Name: ' . $user->name
         . ' Email: ' . $user->email . '<br />' . PHP_EOL;

As seen above, once an instance of the database adapter is created via the corresponding factory, the object is injected straight into the user mapperís constructor. Again, itís valid to point out that the use of this low-level factory is entirely optional, so feel free to skip it if you want. In either case, this example shows pretty clearly how the use of Dependency Injection along with some simple conventions may be of great help when it comes to removing Singletons from an application, library or framework.

Final Thoughts

In this two-part tutorial, I used a fairly realistic example to demonstrate that the elimination of Singletons (and their associated side effects) is a pretty straightforward process that can be successfully tackled with a proper mixture of different programming methodologies and guidelines, such as Inversion of Control and the Separation of Concerns Principle.

Therefore, if you tired of dealing with Singletons, and are looking for an approach that lets you get rid of them in a pretty elegant and efficient way, the examples coded earlier hopefully will point you in the right direction.

See you in the next PHP tutorial!

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials