English 中文(简体)
Singleton pattern + __construct in PHP4
原标题:

To clarify:

  • no I can t make this pure PHP5
  • yes this code works in PHP 4.3.9.

I don t have any real world experience implementing or supporting PHP4 but I had to recently change my class so it supports both PHP4 and PHP5.

  • Can anyone foresee any possible issues in regards to my implementation of the singleton method used here? I only want one instance of this class.
  • I know I could completely get rid of the __construct method, should I? ( and move the function body of it to the Conf function ).
  • Could I have used self anywhere? I wasn t sure of its PHP4 support.

<?php

class Conf {
    function Conf( $filename ) {
        $this->__construct( $filename );
    }

    function __construct( $filename ) {
        echo $filename;
    }

    function getInstance( $filename ) {
        static $instance = null;
        if ( $instance == null ) {
            $instance = new Conf( $filename );
        }
        return $instance;
    }
}

$x = Conf::getInstance(  file.xml  );
最佳回答

To get around the unavailability of self in PHP4, you should change your getInstance method to the following:

class Conf {
    function Conf( $filename ) {
        $this->__construct( $filename );
    }

    function getInstance( $filename ) {
        static $instance = null;
        if ( $instance == null ) {
            $class = __CLASS__;
            $instance = new $class( $filename );
        }
        return $instance;
    }

    // Don t forget to block cloning
    function __clone() {
        trigger_error("Cannot clone a singleton", E_USER_WARNING);
    }
}

Edit: Because __CLASS__ will always be the class name of the class in which the function was defined, to support inheritance, you must redefine the getInstance method in every child class.

class Conf2 extends Conf {
    function newMethod() { echo "Do something"; }

    function getInstance( $filename ) {
        static $instance = null;
        if ( $instance == null ) {
            $instance = new self($filename);
        }
        return $instance;
    }
}

It s a drag, yes, but if you use the above method, you can just copy and paste. I believe this is fixed in PHP 5.3 with late static binding, but I haven t installed it yet to confirm.

问题回答

Your code is unnecessarily complex. You don t need the __construct method at all if you have the other one. (or vise versa, however, older versions of php don t support __construct)

Also, to implement the singleton pattern properly, the static instance should be class scope, and the constructor should be private.

class Singleton {
    private static $instance;

    private function Singleton(){
        //do stuff
    }

    function getInstance(){
        if(self::$instance == null){
            self::$instance = new Singleton();
        }

        return self::$instance;
    }
}

This article describes how to implement the Singleton Pattern in PHP4, and it s problems.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签