0

PHP でシングルトン クラスを作成しました。

<?php
class DataManager
{
    private static $dm;

    // The singleton method
    public static function singleton()
    {
        if (!isset(self::$dm)) {
            $c = __CLASS__;
            self::$dm = new $c;
        }

        return self::$dm;
    }

    // Prevent users to clone the instance
    public function __clone()
    {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
    public function test(){
        print('testsingle');
        echo 'testsingle2';
   }

    function __get($prop) {
        return $this->$prop;
    }

    function __set($prop, $val) {
        $this->$prop = $val;
    }
}
?>

index.php でこのクラスを使用しようとすると、次のようになります。

<?php
include('Account/DataManager.php');

echo 'test';
$dm = DataManager::singleton();
$dm->test();

echo 'testend';
?>

私が得る唯一のエコーは、シングルトンクラスの関数test()である「test」であり、決して呼び出されないようです。また、index.php の最後にある「testend」も呼び出されません。

シングルトン クラスにエラーはありますか?

4

1 に答える 1