note 105670 added to reflectionclass.newinstancewithoutconstructor
| From: | alejosimonatgmail at osu1 dot php dot net | Date: | Tue, 06 Sep 2011 13:41:25 +0000 |
| Subject: | note 105670 added to reflectionclass.newinstancewithoutconstructor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182967@lists.php.net to get a copy of this message | ||
A good first use for this new method.
It implements a transparent parser constructor argument to achieve 99% reusable component.
<?php
use ReflectionClass ;
trait TSingleton
{
/**
* Constructor.
*/
protected function __construct() {}
/**
* Drop clone singleton objects.
*/
protected function __clone() {}
/**
* Gets only one instance.
*
* @params Optional multiple values as arguments for the constructor.
* @return Object instance from called class.
*/
public static function getInstance()
{
static $instance = null ;
if ( ! $instance )
{
$ref = new ReflectionClass( get_called_class() ) ;
$ctor = $ref->getConstructor() ;
// Thanks PHP 5.4
$self = $ref->newInstanceWithoutConstructor() ;
// The magic.
$ctor->setAccessible( true ) ;
$instance = $ctor->invokeArgs( $self, func_get_args() ) ;
}
return $instance ;
}
}
?>
----
Server IP: 201.216.232.3
Probable Submitter: 186.23.111.32
----
Manual Page -- http://www.php.net/manual/en/reflectionclass.newinstancewithoutconstructor.php
Edit -- https://master.php.net/note/edit/105670
Del: integrated -- https://master.php.net/note/delete/105670/integrated
Del: useless -- https://master.php.net/note/delete/105670/useless
Del: bad code -- https://master.php.net/note/delete/105670/bad+code
Del: spam -- https://master.php.net/note/delete/105670/spam
Del: non-english -- https://master.php.net/note/delete/105670/non-english
Del: in docs -- https://master.php.net/note/delete/105670/in+docs
Del: other reasons-- https://master.php.net/note/delete/105670
Reject -- https://master.php.net/note/reject/105670
Search -- https://master.php.net/manage/user-notes.php