Home arrow Blog

In defence of singleton

It has become fashionable to condemn the singleton pattern. Apart from specific complaints, it is said to be not OO. Now what I'm going to provide is not a general response to general remarks, it is a PHP specific response to attacks on singleton. It's based on my own actual development experience.

#128004 • 06/09/2012 3:45pm by Martin Brampton • Vote: Up votes (179) Down votes (129)

Analysis of URL

If your PHP software is going to be installed by other people without any personal intervention, some tricky problems arise. You probably need to figure out how the site is being accessed - it is not just a matter of domain names pointing to web sites. There can be different mechanisms for access, such as http or https, there can be use of non-standard ports. Also, software may be installed in a subdirectory rather in the document root.

We assume that the received URI has not been subjected to a SEF transformation, or that it has already been returned to its standard form. The assumption is also made that entry to the software is always through a file that is index.php or something like index2.php (that is a single digit added to 'index'). This is often a sensible practice, and provides an easy way to protect against direct execution of PHP code using the web server configuration (rather than relying on putting blocking code into all PHP code files).

    $splituri = preg_split('#/index[0-9]?\.php#', $_SERVER['REQUEST_URI']);
    $subdirectory = dirname($splituri[0]);
    if (1 == strlen($subdirectory)) $subdirectory = '';
    $config['subdirlength'] = strlen($subdirectory);

At this point, we've figured out how much of the URI is subdirectory - if length was one, it would have been only a slash.

    $scheme = isset($_SERVER['HTTP_SCHEME']) ? 
        $_SERVER['HTTP_SCHEME'] : ((isset($_SERVER['HTTPS']) AND
                    strtolower($_SERVER['HTTPS'] != 'off')) ? 'https' : 'http');

Deriving the scheme was relatively easy, although it had to take account of different ways in which information is presented through the $_SERVER super global. Getting at the port is more difficult:

    if (isset($_SERVER['HTTP_HOST'])) {
        $withport = explode(':', $_SERVER['HTTP_HOST']);
        $servername = $withport[0];
        if (isset($withport[1])) $port = ':'.$withport[1];
    }
    elseif (isset($_SERVER['SERVER_NAME'])) {
        $servername = $_SERVER['SERVER_NAME'];
    }
    else trigger_error(T_('Impossible to determine the name of this server'),
        E_USER_ERROR);
    if (!isset($port) AND !empty($_SERVER['SERVER_PORT'])) {
        $port = ':'.$_SERVER['SERVER_PORT'];
    }
    if (isset($port)) {
        if (($scheme == 'http' AND $port == ':80') OR 
        ($scheme == 'https' AND $port == ':443')) $port = '';
    }
    else $port = '';

Note that there is a possibility of failing to obtain the name of the server, which results in an error. Otherwise, we've got everything broken up, which allows us to save information about the site URI as accessed and to deduce what would be the 'secure' equivalent, if available:

    $afterscheme = '://'.$servername.$port.$subdirectory;
    $config['live_site'] = $config['secure_site'] = 
        $_SESSION['aliro_live_site'] = $scheme.$afterscheme;
    $config['unsecure_site'] = $_SESSION['aliro_unsecure_site'] 
        = 'http'.$afterscheme;

The code is believed to work reliably in a variety of circumstances, but if you know better, please add your comment!

#128011 • 02/23/2011 3:23pm by Martin Brampton • Vote: Up votes (141) Down votes (110)

Useful command for Subversion

If you develop software, it's quite likely that you use Subversion. I found the following command at Snipplr and wanted to record it so that I could easily find it again.

Sometimes it is convenient to remove all the Subversion hidden files from a directory structure - it isn't always convenient to export from the repository. Provided you are using Linux, the command below will do it:

 find . -name ".svn" -exec rm -rf {} \;

#128010 • 12/24/2010 6:09pm by Martin Brampton • Vote: Up votes (134) Down votes (105)

MVC Problems in PHP

Once people are aware of it, few disagree with the general idea of MVC. But differing interpretations and practical implementation problems tend to create disputes. I want to discuss the particular problems of creating HTML in PHP.

#128009 • 10/15/2010 5:18pm by Martin Brampton • Vote: Up votes (148) Down votes (93)

Autoload in complex systems

It's very tedious keeping track of code modules, and the PHP 5 ability to autoload them is a great boon. But in a complex system, such as a CMS, multiple software applications have to coexist, and this places demands on autoload techniques. In some environments, the system autoloader must come last.

#128008 • 07/12/2010 9:32am by Martin Brampton • Vote: Up votes (140) Down votes (95)

Joomla Session Problem

Please don't think I spend all my time looking at Joomla. But a friend was struggling with a problem where every attempt to login to the admin side of a site failed to achieve a login, but also failed to show any kind of diagnostic. What could block the Joomla admin login?

#128007 • 06/03/2010 4:15pm by Martin Brampton • Vote: Up votes (138) Down votes (116)

Another Joomla installer problem

There is a problem with the PHP function gunzip() which in early versions of PHP 5 is known to have made large and unnecessary memory demands in some circumstances. This error is liable to be triggered by Joomla for some PHP versions in the 5.1.x range. Also, there is a problem with some of the Joomla code.

#128006 • 10/20/2009 5:26pm by Martin Brampton • Vote: Up votes (152) Down votes (115)

Well used singletons are GOOD!

Pejorative comments about singleton classes abound. All they seem to me to have proved is the age old truth of software: all techniques can be used badly. Earlier, I looked at Steve Yegge's complaints about singletons. Now I'm reviewing a set of objections attributed to Brian Button by Scott Densmore.

#128005 • 09/19/2009 1:00pm by Martin Brampton • Vote: Up votes (151) Down votes (113)

Using powerful PHP array functions

There is a wealth of useful power in the PHP array functions. They are generally much more efficient than writing your own PHP code, since they are compiled. Not that I'm against PHP being an interpreted language, there are a lot of advantages in that. But you should still take advantage of efficiency where it is easily available.

#128003 • 09/17/2009 10:24pm by Martin Brampton • Vote: Up votes (150) Down votes (111)

Joomla extension installer memory failures

The Joomla! installer for adding extensions can take excessive amounts of memory. So much so that your PHP memory may be exceeded, causing the install to fail. A significant part of the problem is a coding error that can be easily fixed.

#128002 • 08/30/2009 8:34am by Martin Brampton • Vote: Up votes (144) Down votes (105)

Web Software on a VPS

Much of my time is spent as a developer of web related software. But recently I've found it impossible to avoid being drawn into some quite complex hosting issues - for handling my own sites and those of clients. Experts in internet related software will be familiar with the problems I have found; many software developers will be less familiar with them. This article is intended to help those who, like me, approach the use of hosting with less than comprehensive knowledge of the area.

#128001 • 08/28/2009 4:03pm by Martin Brampton • Vote: Up votes (155) Down votes (117)

Results 1 to 11 of 11