PHP creates objects without calling the constructor

PHP creates objects without calling the constructor

This requirement can be achieved using PHP’s reflection feature, ReflectionClass :: newInstanceWithoutConstructor.

Example:

<?php
class a
{
    public $foo=0;
    public $bar=9;
    public function __construct()
    {
        $this->foo=1;
        echo "Call the constructor\n";
    }
}
  
  $ref = new ReflectionClass('a');
  $inst = $ref->newInstanceWithoutConstructor();
  print_r($inst);

The above output:

a Object
(
    [foo] => 0
    [bar] => 9
)

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *