Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singletonパターン #3 #4

merged 1 commit into from Feb 24, 2019
Changes from all commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+49 −0
Diff settings


Just for now

Copy path View file
@@ -0,0 +1,16 @@
require __DIR__ . '/vendor/autoload.php';
use DP\Singleton\Singleton;
//echo "Singletonクラスを普通にnewしてみる。\n";
//new Singleton(); //たぶんおこられる。
echo "getInstanceしてみる(instance1)\n";
$instance1 = Singleton::getInstance();
echo "もう一度、getInstanceしてみる(instance2)\n";
$instance2 = Singleton::getInstance();
echo "2回目のgetInstanceはインスタンス生成を行わないため、constructを通らないはず\n";
echo "(instance1 === instance2)なのか確認\n";
echo ($instance1 === $instance2) . "\n";
Copy path View file
@@ -0,0 +1,33 @@
namespace DP\Singleton;
class Singleton
* @var Singleton
private static $singleton;
//他のクラスからnew Singletonをするとエラーになる。
private function __construct()
echo "create SingletonClassInstance\n";
public static function getInstance(): Singleton
if (is_null(self::$singleton)) {
self::$singleton = new Singleton();
return self::$singleton;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.