Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Singleton/Sample/Singleton.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Sockstack\DesignPattern\Singleton\Sample;

class Singleton
Expand All @@ -9,11 +10,10 @@ class Singleton
//将构造函数私有化
private function __construct()
{

}

//获取实例
static public function getInstance()
public static function getInstance()
{
return empty(static::$instance) ? static::$instance = new static() : static::$instance;
}
Expand All @@ -30,4 +30,3 @@ public function __wakeup()
trigger_error('对象不能被反序列化');
}
}

11 changes: 6 additions & 5 deletions tests/SingletonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* Created by PhpStorm.
* User: sockstack
* Date: 2018/11/6
* Time: 23:36
* Time: 23:36.
*/

namespace Sockstack\DesignPattern\Test;

use PHPUnit\Framework\TestCase;
Expand All @@ -18,7 +19,7 @@ class SingletonTest extends TestCase
public function testSingletonByNew()
{
$object = new Singleton();
$this->fail("create fail");
$this->fail('create fail');
}

/**
Expand All @@ -30,7 +31,7 @@ public function testSingletonClone()
$object = Singleton::getInstance();
$clone_objecdt = clone $object;

$this->fail("clone fail");
$this->fail('clone fail');
}

/**
Expand All @@ -42,7 +43,7 @@ public function testSingletonUnserialize()
$object = Singleton::getInstance();
$unserialize_object = unserialize(serialize($object));

$this->fail("unserialize fail");
$this->fail('unserialize fail');
}

public function testSingleton()
Expand All @@ -52,4 +53,4 @@ public function testSingleton()

$this->assertEquals($object1, $object2);
}
}
}