note 124501 added to class.weakreference
| From: | SandorToth@php.net | Date: | Wed, 11 Dec 2019 16:09:45 +0000 |
| Subject: | note 124501 added to class.weakreference | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-213508@lists.php.net to get a copy of this message | ||
You might consider to use WeakReference in your Container class. Don't forget to create the
object into a variable and pass the variable to WeakReference::create() otherwise you going to
->get() null.
Consider as wrong solution, which returns null
<?php
/**
* @return App
*/
public static function app() : App
{
if (!static::$app) {
static::$app = WeakReference::create(new App());
}
return static::$app->get();
}
?>
Consider as GOOD solution, which returns App instance
<?php
/**
* @return App
*/
public static function app() : App
{
if (!static::$app) {
$app = new App();
static::$app = WeakReference::create($app);
}
return static::$app->get();
}
?>
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.195 (proxied: 86.4.204.248)
----
Manual Page -- http://php.net/manual/en/class.weakreference.php
Edit -- https://master.php.net/note/edit/124501
Del: integrated -- https://master.php.net/note/delete/124501/integrated
Del: useless -- https://master.php.net/note/delete/124501/useless
Del: bad code -- https://master.php.net/note/delete/124501/bad+code
Del: spam -- https://master.php.net/note/delete/124501/spam
Del: non-english -- https://master.php.net/note/delete/124501/non-english
Del: in docs -- https://master.php.net/note/delete/124501/in+docs
Del: other reasons-- https://master.php.net/note/delete/124501
Reject -- https://master.php.net/note/reject/124501
Search -- https://master.php.net/manage/user-notes.php