News Feed
Jobs Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Allan MacGregor:
Design Patterns in PHP Singletons
January 29, 2014 @ 09:25:55

Allan MacGregor has posted his latest in his look at design patterns in PHP with this most recent post about the Singleton pattern.

The singleton pattern is used to restrict the instantiation of a class to a single object, which can be useful when only one object is required across the system. Singletons are designed to ensure there is a single (hence the name singleton) class instance and that is global point of access for it, along with this single instance we have global access and lazy initialization.

He provides a basic Singleton implementation in PHP, a "User" class that always returns the same instance of itself no matter how many times the "singleton" method is called. He continues on and touches on one of the pain points around singleton use - many developers consider them an anti-pattern because their results can make it difficult to correctly test. He talks about how they break the Single Responsibility Principle (part of SOLID) and how they can hide dependency injection.

Singletons, Anti-patterns, and patterns in general are not good or bad; what makes a Singleton an Anti-pattern is not the pattern itself but how often is poorly implemented and how easy it is to do so.
0 comments voice your opinion now!
design pattern singleton introduction antipattern testing

Link: http://coderoncode.com/2014/01/27/design-patterns-php-singletons.html

Russell Walker:
Handling Global Data in PHP Web Applications
September 16, 2013 @ 12:31:07

Russell Walker has a post on his site sharing some suggestions about effectively dealing with global data in your PHP applications.

Almost every web application needs to handle global data. There are certain things that just have to be available throughout the entire code base, such as database connections, configuration settings, and error handling routines. As a PHP developer, you may have heard the mantra 'globals are evil', but this naturally begs the question 'what should I use instead of global variables?'

He includes four different options (five including the actual use of global variables):

  • Static classes
  • Singleton
  • Registry
  • Dependency injection

For each of the options he includes summaries of both the advantages and disadvantages as well as some sample code showing their use. Ultimately, he points out that it's up to the developer of the application which option fits best.

0 comments voice your opinion now!
global variable data opinion options registry singleton dependencyinjection static

Link: http://russellscottwalker.blogspot.co.uk/2013_09_07_archive.html

Codeception.com:
Nothing is Untestable AspectMock in Action
August 01, 2013 @ 11:26:12

On the Codeception site they've posted a guide to using the AspectMock feature of the testing tool to prove that "nothing is untestable."

We already announced AspectMock, the mocking framework that may dramatically change the way you do testing in PHP. In this video Jeffrey Way shows how AspectMock is different from others. In this post we will demonstrate its powers too, and we will try to break some stereotypes about PHP testing. To get the code tested, you should always keep in mind how you would write a test for it. We know unit testing requires some good practices to follow and bad practices to avoid.

Their first example involves testing singletons, notorious for being difficult to test because of their "global" state. He also gives a more practical example using a Yii2-based application and a login form. True to its name, the AspectMock uses Aspect Oriented Programming concepts to make the "magic" happen behind the scenes.

0 comments voice your opinion now!
codeception aspect mock aspectoriented unittest singleton

Link: http://codeception.com/07-31-2013/nothing-is-untestable-aspect-mock.html

Zumba Engineering Blog:
Mocking Singleton PHP classes with PHPUnit
November 26, 2012 @ 09:51:04

On the Zumba Engineering blog today Chris Taylor has a new post about mocking in PHPUnit, specifically how to handle those pesky Singleton methods lurking around your codebase.

In many of our projects, utilities and vendor classes are implemented with a singleton pattern. [...] In this post, we'll cover a nice way to inject a PHPUnit mock object for use in testing methods that utilize singleton classes.

He starts by introducing mocking and how to use mock classes in PHPUnit with a simple "sayHello" example. Adding on another layer, he creates a "SomeclassMock" class, defining its own "expects" and "cleanup" methods. This class forces the Singleton method to act more like a regular non-static method and "resets" it after each use.

0 comments voice your opinion now!
mocking phpunit class singleton expects cleanup tutorial


Gonzalo Ayuso:
The reason why singleton is a "problem" with PHPUnit
September 24, 2012 @ 11:57:02

Gonzalo Ayuso has a new post that responds to the idea that "singletons are a problem when testing" your applications with something like PHPUnit.

Maybe this pattern is not as useful as it is in J2EE world. With PHP everything dies within each request, so we cannot persist our instances between requests (without any persistent mechanism such as databases, memcached or external servers). But at least in PHP we can share the same instance, with this pattern, in our script.

He illustrates a bad side effect of this sharing of resources with a simple unit test that increments a counter in a class. He notes that, because the script shares the object, you can't reliably know the state of it as you don't know what's happened before your use. He recommends two things to help the situation - either not use them at all or destroy the instance each time after using it (counterproductive to using a Singleton, obviously).

0 comments voice your opinion now!
singleton designpattern problem resource sharing


PHPEasy.co.uk:
Design Patterns - The Singleton Pattern
July 16, 2012 @ 10:54:40

PHPEasy.co.uk has a new tutorial posted introducing one of the most common (and often misused) design patterns out there - the Singleton pattern.

In this first tutorial in the design pattern series we are going to investigate and implement the singleton design pattern. [...] A design pattern is a common solution to a given problem, problems in programming tend to recur and we often find ourselves trying to solve the same issues over and over. The common techniques that provide solutions to these problems can be referred to as design patterns.

He talks about some of the most common uses for the Singleton pattern (mainly replacing a global variable) and includes an example of using one to fetch a database object.

0 comments voice your opinion now!
singleton designpattern tutorial example


PHP-Tip-a-Day:
PHP Tutorial The Legend of the Singleton
June 13, 2012 @ 09:02:36

Following his recent allegory about the Factory pattern (as described in story form) Greg Bulmash has posted the Legend of the Singleton to help with your understanding this pattern.

The Singleton pattern provides an interface to let your application always pull out the same object (or make a new one if it needs to).

His legend talks about kings, mythological data sources and the overloading of multiple "hoses" (connections) to it. Also included is a code example showing a simple database class that includes a "getInstance" method acting as the Singleton to return either a new or the (same) current instance.

0 comments voice your opinion now!
legend story singleton designpattern


Charles Sprayberry's Blog:
DI and global state
January 31, 2012 @ 09:24:47

In response to some of the comments made on his previous post about why you should use dependency injection in your applications, Charles Sprayberry is back with some more concrete examples showing how it all works with some code to back it up.

To help better explain each of the three aspects of DI I discussed in the previous article I'll be going over each more thoroughly and with those code examples requested. I'll be going through each point one at a time as the explanations will likely be of some length compared to the original post.

He starts with the "villain" of the story - the Singleton design pattern, a difficult to test method that lulls you into thinking you're not in the global scope. He talks about the problem of using this approach and how the Factory design pattern can be used to create an alternative. He changes up the example to create a "DbTableFactory" class that can be used to create the objects needed - in this case a "UserTable" object with the connection injected into it at construct time.

0 comments voice your opinion now!
dependency injection di factory singleton global designpattern


DevArticles.com:
Singletons in PHP
December 06, 2011 @ 10:17:33

On DevArticles.com today there's a new tutorial posted talking about one of the more popular design patterns, the Singleton, and how it can be implemented in PHP.

Though in the past they enjoyed both popularity and a certain amount of prestige, without a doubt Singletons have progressively become one of the most evil and despicable villains in object-oriented design. Singletons earned their bad reputation for a reason: bringing them to life requires the programmer to deal at least with a static method. This is simply an elegant masquerade for creating a global access point (which in most cases is mutable as well) throughout an entire application. And we all know that global, mutable access is unquestionably a bad thing that must be avoided at all costs.

In this first part (of two) of the series they introduce the Singleton pattern and show how, via an example of using a database adapter interface to work with a MySQL database, in a tightly coupled example. In the second part of the series, they'll show how to break these apart using dependency injection.

0 comments voice your opinion now!
singleton designpattern dependencyinjection di mysql database adapter interface


Ryan Gantt's Blog:
Horizontal reusability with traits in PHP 5.4
August 24, 2011 @ 10:42:42

Ryan Gantt has a new tutorial posted to his blog today looking at one of the features in the upcoming PHP 5.4.x releases - traits. Specifically he looks at the horizontal reusabillity they allow for in your applications.

The ability for a class to inherit from multiple parents is maligned by many, but can be a good thing in some situations. For those working in PHP, multiple inheritance has never been an option; classes are limited to one parent, though they can implement many other datatypes through the use of interfaces. Interfaces can lead to code duplication in improperly-factored inheritance hierarchies. Even in well-architected hierarchies, multiple classes that implement similar methods can contain a lot of overlap.

He starts with a definition of what traits are and where their real usefulness is (as well as what should be the difference between a class and a trait). He gives an example of a typical hierarchy where two classes extend a parent but then they both need the same functionality. Code duplication's not a possibility and inheritance make run into exposure issues. Traits come to the rescue by dropping in just the feature you need when you need it. His example code shows adding some logging to a simple class via a "Logging" trait and a "Singleton trait" example.

0 comments voice your opinion now!
horizontal reusability traits singleton logging tutorial introduction



Community Events











Don't see your event here?
Let us know!


hhvm introduction package application example language framework hack project security facebook component database unittest podcast release install symfony2 performance composer

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework