mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-11 16:23:57 +02:00
Merge pull request #273 from simonrjones/fix/global-functions
Removing \ from global functions
This commit is contained in:
@@ -82,7 +82,7 @@ class Singleton
|
|||||||
{
|
{
|
||||||
static $instance = null;
|
static $instance = null;
|
||||||
if (null === $instance) {
|
if (null === $instance) {
|
||||||
$instance = new static;
|
$instance = new static();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $instance;
|
return $instance;
|
||||||
@@ -122,12 +122,12 @@ class SingletonChild extends Singleton
|
|||||||
}
|
}
|
||||||
|
|
||||||
$obj = Singleton::getInstance();
|
$obj = Singleton::getInstance();
|
||||||
\var_dump($obj === Singleton::getInstance()); // bool(true)
|
var_dump($obj === Singleton::getInstance()); // bool(true)
|
||||||
|
|
||||||
$anotherObj = SingletonChild::getInstance();
|
$anotherObj = SingletonChild::getInstance();
|
||||||
\var_dump($anotherObj === Singleton::getInstance()); // bool(false)
|
var_dump($anotherObj === Singleton::getInstance()); // bool(false)
|
||||||
|
|
||||||
\var_dump($anotherObj === SingletonChild::getInstance()); // bool(true)
|
var_dump($anotherObj === SingletonChild::getInstance()); // bool(true)
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
The code above implements the singleton pattern using a [*static* variable](http://php.net/language.variables.scope#language.variables.scope.static) and the static creation method `getInstance()`.
|
The code above implements the singleton pattern using a [*static* variable](http://php.net/language.variables.scope#language.variables.scope.static) and the static creation method `getInstance()`.
|
||||||
@@ -135,7 +135,7 @@ Note the following:
|
|||||||
|
|
||||||
* The constructor [`__construct`](http://php.net/language.oop5.decon#object.construct) is declared as protected to prevent creating a new instance outside of the class via the `new` operator.
|
* The constructor [`__construct`](http://php.net/language.oop5.decon#object.construct) is declared as protected to prevent creating a new instance outside of the class via the `new` operator.
|
||||||
* The magic method [`__clone`](http://php.net/language.oop5.cloning#object.clone) is declared as private to prevent cloning of an instance of the class via the [`clone`](http://php.net/language.oop5.cloning) operator.
|
* The magic method [`__clone`](http://php.net/language.oop5.cloning#object.clone) is declared as private to prevent cloning of an instance of the class via the [`clone`](http://php.net/language.oop5.cloning) operator.
|
||||||
* The magic method [`__wakeup`](http://php.net/language.oop5.magic#object.wakeup) is declared as private to prevent unserializing of an instance of the class via the global function [`\unserialize()`](http://php.net/function.unserialize).
|
* The magic method [`__wakeup`](http://php.net/language.oop5.magic#object.wakeup) is declared as private to prevent unserializing of an instance of the class via the global function [`unserialize()`](http://php.net/function.unserialize).
|
||||||
* A new instance is created via [late static binding](http://php.net/language.oop5.late-static-bindings) in the static creation method `getInstance()` with the keyword `static`. This allows the subclassing of the class `Singleton` in the example.
|
* A new instance is created via [late static binding](http://php.net/language.oop5.late-static-bindings) in the static creation method `getInstance()` with the keyword `static`. This allows the subclassing of the class `Singleton` in the example.
|
||||||
|
|
||||||
The singleton pattern is useful when we need to make sure we only have a single instance of a class for the entire
|
The singleton pattern is useful when we need to make sure we only have a single instance of a class for the entire
|
||||||
|
Reference in New Issue
Block a user