Doc #78451 [Opn->Nab]: No possibility make Singleton in the PHP

From: Date: Sat, 24 Aug 2019 16:53:08 +0000
Subject: Doc #78451 [Opn->Nab]: No possibility make Singleton in the PHP
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-16903@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78451&edit=1 ID: 78451 Updated by: requinix@php.net Reported by: forziforzi at gmail dot com Summary: No possibility make Singleton in the PHP -Status: Open +Status: Not a bug Type: Documentation Problem Package: Class/Object related PHP Version: 7.3.8 Block user comment: N Private report: N New Comment: PHP allows you to shoot yourself in the foot if you wish. Previous Comments: ------------------------------------------------------------------------ [2019-08-24 11:56:37] forziforzi at gmail dot com Description: ------------ --- From manual page: https://php.net/class.closure --- So, no Singleton in the PHP! Test script: --------------- <?php final class A { private function __construct() {} private function __clone() { throw new \Exception('Singleton'); } private function __sleep() { throw new \Exception('Singleton'); } private function __wakeup() { throw new \Exception('Singleton'); } public static function instantiate() { static $obj; return $obj = $obj ?? new self; } } $closure = function () { $class = get_class($this); return new $class; }; $obj = A::instantiate(); var_dump($obj); var_dump($closure->call($obj)); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78451&edit=1

« previous php.doc.bugs (#16903) next »