ReflectionClass
在线手册:中文 英文
PHP手册

ReflectionClass::newInstanceWithoutConstructor

(PHP >= 5.4.0)

ReflectionClass::newInstanceWithoutConstructorCreates a new class instance without invoking the constructor.

说明

public object ReflectionClass::newInstanceWithoutConstructor ( void )

Creates a new instance of the class without invoking the constructor.

参数

返回值

错误/异常

A ReflectionException if the class is an internal class that cannot be instantiated without invoking the constructor.

参见


ReflectionClass
在线手册:中文 英文
PHP手册
PHP手册 - N: Creates a new class instance without invoking the constructor.

用户评论:

oliver at ananit dot de (24-Nov-2011 11:57)

If this method is not available in your version of PHP you can use a trick to create an instance without calling the constructor.
Use reflection to get the properties and default values of the class, and create a fake "serialized" string.

<?php
function createInstanceWithoutConstructor($class){
   
$reflector = new ReflectionClass($class);
   
$properties = $reflector->getProperties();
   
$defaults = $reflector->getDefaultProperties();
           
   
$serealized = "O:" . strlen($class) . ":\"$class\":".count($properties) .':{';
    foreach (
$properties as $property){
       
$name = $property->getName();
        if(
$property->isProtected()){
               
$name = chr(0) . '*' .chr(0) .$name;
            } elseif(
$property->isPrivate()){
               
$name = chr(0)  . $classchr(0).$name;
            }
           
$serealized .= serialize($name);
            if(
array_key_exists($property->getName(),$defaults) ){
               
$serealized .= serialize($defaults[$property->getName()]);
            } else {
               
$serealized .= serialize(null);
            }
        }
   
$serealized .="}";
    return
unserialize($serealized);
}
?>

Example:

<?php
class foo
{
    public
$a = 10;
    protected
$b = 2;
    private
$c = "default";
    protected
$d;
    public function
__construct(){
       
$this->a = null;
       
$this->b = null;
       
$this->c = "constructed";
       
$this->d = 42;
    }
}

var_dump(createInstanceWithoutConstructor('foo'));
?>

Output:
object(foo)#6 (4) {
  ["a"]=>
  int(10)
  ["b":protected]=>
  int(2)
  ["c":"foo":private]=>
  string(7) "default"
  ["d":protected]=>
  NULL
}

I hope this can help someone.
Oliver Anan

alejosimon at gmail (06-Sep-2011 02:41)

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 ;
    }
}

?>