This library provides a way of avoiding usage of constructors when instantiating PHP classes.

Last update: May 21, 2022

Instantiator

This library provides a way of avoiding usage of constructors when instantiating PHP classes.

Build Status Code Coverage Dependency Status

Latest Stable Version Latest Unstable Version

Installation

The suggested installation method is via composer:

php composer.phar require "doctrine/instantiator:~1.0.3"

Usage

The instantiator is able to create new instances of any class without using the constructor or any API of the class itself:

$instantiator = new \Doctrine\Instantiator\Instantiator();

$instance = $instantiator->instantiate(\My\ClassName\Here::class);

Contributing

Please read the CONTRIBUTING.md contents if you wish to help out!

Credits

This library was migrated from ocramius/instantiator, which has been donated to the doctrine organization, and which is now deprecated in favour of this package.

GitHub

https://github.com/doctrine/instantiator
Comments
  • 1. Minimum PHP version change in minor is breaking dependents

    I am using phpunit on PHP 7.0.n. The last PHP version bump breaks this https://github.com/sebastianbergmann/phpunit-mock-objects/issues/371

    If we could not bump minimum PHP versions in minors that would be great. ❤️

    Reviewed by PeeHaa at 2017-08-01 14:23
  • 2. Can't instantiate final class extending ArrayIterator

    final class Foo extends ArrayIterator
    {
    }
    
    (new \Doctrine\Instantiator\Instantiator()->instantiate(Foo::class);
    
    PHP Fatal error:  Uncaught Exception: unserialize(): Error at offset 13 of 14 bytes in /Users/cmcnulty/Documents/Code/phpspec/vendor/doctrine/instantiator/src/Doctrine/Instantiator/Exception/UnexpectedValueException.php:76
    

    Reproduced on 1.0.5 and 1.1.0 and, not had a chance to debug. Was reported to PhpSpec as https://github.com/phpspec/phpspec/issues/1124

    Reviewed by ciaranmcnulty at 2017-10-15 11:07
  • 3. HHVM fails to clone DateTime [Failing test only]

    No idea...!

    It looks like HHVM dies when trying to clone a DateTime that has been instantiated with instantiator.

    [email protected]:instantiator:failing-hhvm-test$ hhvm --version
    HipHop VM 3.3.0 (rel)
    Compiler: tags/HHVM-3.3.0-0-g0a3cfb87b8a353fc7e1d15374f4adc413e37aba9
    Repo schema: 9a391d9a03e15fccba1cde6d35c05b7cdd380238
    Extension API: 20140829
    [email protected]:instantiator:failing-hhvm-test$ hhvm vendor/bin/phpunit
    PHPUnit 4.3.1 by Sebastian Bergmann.
    
    Configuration read from /home/davem/src/instantiator/phpunit.xml.dist
    
    ..........
    Fatal error: A null object pointer was used. in /home/davem/src/instantiator/src/Doctrine/Instantiator/Instantiator.php on line 73
    

    Though it's happy to do so with a DateTime that has been constructed normally:

    [email protected]:instantiator:failing-hhvm-test$ cat t.php
    <?php
    
    $a = new DateTime();
    $b = clone $a;
    
    echo $b->format('r');
    [email protected]:instantiator:failing-hhvm-test$ hhvm t.php
    Mon, 06 Oct 2014 23:28:45 +0100%
    
    Reviewed by davedevelopment at 2014-10-06 22:30
  • 4. version 1.1.0 broke phpunit on PHP 5.4 ~ PHP 5.6

    the changes on 1.1.0, such as

    syntax error, unexpected ':', expecting ';' or '{' 
    
    at vendor/doctrine/instantiator/src/Doctrine/Instantiator/Instantiator.php:95
    

    broke phpunit on PHP 5.4 ~ PHP 5.6

    Here's the dependency tree:

    phpunit/phpunit 4.8.24 The PHP Unit Testing framework.
    ├──ext-dom *
    ├──ext-json *
    ├──ext-pcre *
    ├──ext-reflection *
    ├──ext-spl *
    ├──php >=5.3.3
    ├──phpspec/prophecy ^1.3.1
    │  ├──doctrine/instantiator ^1.0.2
    │  │  └──php >=5.3,<8.0-DEV
    

    I wonder to know how to force composer to install the old release of doctrine/instantiator ^1.0.2.

    Reviewed by kinosang at 2017-07-27 07:55
  • 5. Unable to instanciate Symfony\Component\HttpFoundation\File\File

    Hey,

    Currently i'm unable to test (via instanciator) File object or any object that extends this class because Instanciator failed :(

    Given error :

    Could not produce an instance of "Domain\Mail\Attachment" via un-serialization, since an error was triggered in file "vendor/doctrine/instantiator/src/Doctrine/Instantiator/Instantiator.php" at line "184" (Doctrine\Instantiator\Exception\UnexpectedValueException)
    

    Have you an idea, how deal with object containing a Resource ?

    Reviewed by jjsaunier at 2015-11-18 11:35
  • 6. Instantiator attempts to clone classes that are not cloneable

    I don't know how this might be solved, but some builtin classes are not cloneable:

    PHP 5.6.0 (cli) (built: Oct  4 2014 20:41:37)
    Copyright (c) 1997-2014 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
        with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
        with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
    
    <?php
    
    require "vendor/autoload.php";
    
    $instantiator = new \Doctrine\Instantiator\Instantiator();
    $instance = $instantiator->instantiate('MongoCollection');
    
    PHP Fatal error:  Trying to clone an uncloneable object of class MongoCollection in /Users/davem/src/instantiator/src/Doctrine/Instantiator/Instantiator.php on line 74
    PHP Stack trace:
    PHP   1. {main}() /Users/davem/src/instantiator/test.php:0
    PHP   2. Doctrine\Instantiator\Instantiator->instantiate() /Users/davem/src/instantiator/test.php:6
    

    I'm not aware of any way in which to tell if something isn't cloneable...

    Reviewed by davedevelopment at 2014-10-04 20:38
  • 7. Cannot instantiate final class if it extends internal

    For some reason in PHP > 5.6 you're checking a class isn't final and internal, instead you should be checking it's not final w/ internal ancestors (as in the case below for PHP > 5.4)

    This is linked to https://github.com/phpspec/phpspec/issues/704

    Reviewed by ciaranmcnulty at 2015-06-13 14:17
  • 8. Adds support of internal child class instantiation

    Related to https://github.com/doctrine/instantiator/issues/39

    This is my attempt to resolve a long on-going issue on the phpspec repository, taking inspiration on the SF VarExporter component.

    I do not see any other missing testcases, however feel free to let me know if you think about some, I will happily add them.

    /cc @ciaranmcnulty @Ocramius @mikeSimonson

    Reviewed by gquemener at 2019-10-21 14:57
  • 9. doctrine/instantiator 1.0.1 requires php ~5.3 -> your PHP version (7.1.8) does not satisfy that

    $ composer req doctrineorm Using version ^1.0 for symfony/orm-pack ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages.

    Problem 1 - doctrine/instantiator 1.0.1 requires php ~5.3 -> your PHP version (7.1.8) does not satisfy that requirement.

    Reviewed by shakaran at 2017-09-15 04:27
  • 10. Dependency problem

    Hello, I wasn't sure if I had to post this here or on doctrine/orm : doctrine/orm 2.5.x-dev requires doctrine/instantiator ~1.0.1 -> no matching package found.

    Reviewed by Soviann at 2017-07-27 08:55
  • 11. Avoid cloning uncloneables

    Fixes #7

    I used XMLReader as I'm pretty sure that's everywhere theses days, and it doesn't have a __clone method.

    The bug @whatthejeff mentioned (https://bugs.php.net/bug.php?id=53967) might bring further weirdness at some point, SplFileObject seems happy to be cloned if it's been instantiated using instantiator, but not if it's a "proper" instance?

    PHP 5.6.0 (cli) (built: Oct  4 2014 20:41:37)
    Copyright (c) 1997-2014 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
        with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
        with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
    
    <?php
    
    require "vendor/autoload.php";
    
    $instantiator = new \Doctrine\Instantiator\Instantiator();
    $instance = $instantiator->instantiate('SplFileObject');
    
    $b = clone $instance;
    
    $a = new SplFileObject(__FILE__);
    $b = clone $a;
    
    PHP Fatal error:  main(): An object of class SplFileObject cannot be cloned in /Users/davem/src/instantiator/test.php on line 11
    PHP Stack trace:
    PHP   1. {main}() /Users/davem/src/instantiator/test.php:0
    
    shell returned 255
    
    Reviewed by davedevelopment at 2014-10-04 21:13
  • 12. Promoted property causing Error

    A value object, mapped as embeddable, perfectly working:

    <?php
    class Foo {
        private ?string $bar = null;
        public function __construct(?string $bar) {
            $this->bar = $bar;
        }
    }
    

    But if you try to promote property:

    <?php
    class Foo {
        public function __construct(private ?string $bar = null) {
        }
    }
    

    It fails with Error: Typed property Foo::$bar must not be accessed before initialization as soon as you try to access property.

    Property is not mapped.

    Edit: tried to downgrade to Doctrine 2.8 to check if could be a problem with 2.9: no changes.

    Reviewed by garak at 2021-05-27 08:13
  • 13. Typehint className in instantiate method

    I think this was not Typehinted for BC break reasons but since in src/Doctrine/Instantiator/Instantiator.php the method instantiate calls the buildAndCacheFromFactory method where className is Typehinted as string , I don't see in which scenario $className could not be a string.

    Moreover, in the interface the $className parameter is already described as string in the annotation.

    If we accept classes that implement InstantiatorInterface but don't use a string as parameter (unlikely) then we need to remove the annotation. Right ?

    Reviewed by Valouleloup at 2018-04-27 10:15
  • 14. What about to add the capability to set properties with given set of values?

    Hello, i would like to use this package in a project where i serialize/deserialize entities in/from json file. I know that when applied to the full ORM it is up to the hydratator to populate the instantiated object with the right values respecting the mapping etc, but when used alone i feel that the package is missing the capability to populate the instantiated object with given values. What do you think about?

    Reviewed by lorenzomar at 2015-08-01 13:07
  • 15. [WIP] #9 - adding phpunit test that checks instantiator against all internal classes

    See #9

    This PR is mainly tracking exotic classes with weird internal data-structures.

    The test is mainly aimed at finding defects and/or unsupported classes, not really fixing anything within Instantiator itself.

    Reviewed by Ocramius at 2014-10-07 03:20
PHP Class converter to namepaces.

Namespacer This tool will assist you in taking older underscore/prefix spaced code and will namespace it as best as it can, and also make the files an

Sep 9, 2021
The web application for PHP Online

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Mar 2, 2022
PHPStan rules for detecting usage of deprecated classes, methods, properties, constants and traits.

Rules for detecting usage of deprecated classes, methods, properties, constants and traits. PHPStan Installation To use this extension, require it in

May 23, 2022
PHPProject is a library written in pure PHP that provides a set of classes to write to different project management file formats

PHPProject PHPProject is a library written in pure PHP that provides a set of classes to write to different project management file formats, i.e. Micr

Feb 28, 2022
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way. ( WEBSITE VERSION )
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way. ( WEBSITE VERSION )

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

Feb 14, 2022
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

Nov 20, 2021
A small library for usage deepai.org api

deepai a small library for usage deepai.org api install via composer : composer require mkhab7/deepai usage use Solid\Deepai\Deepai; require_once 've

Mar 11, 2022
This component changes the way Magento 2 generates Interceptor classes

ABOUT This component changes the way Magento 2 generates Interceptor classes (a mechanism that allows plugins to work together). Instead of generating

May 4, 2022
PhpMetrics provides metrics about PHP project and classes, with beautiful and readable HTML report.
PhpMetrics provides metrics about PHP project and classes, with beautiful and readable HTML report.

PhpMetrics provides metrics about PHP project and classes, with beautiful and readable HTML report.

May 19, 2022
Get the system resources in PHP, as memory, number of CPU'S, Temperature of CPU or GPU, Operating System, Hard Disk usage, .... Works in Windows & Linux

system-resources. A class to get the hardware resources We can get CPU load, CPU/GPU temperature, free/used memory & Hard disk. Written in PHP It is a

Dec 24, 2021
WebDirStat is a disk usage utility for web servers written in PHP
WebDirStat is a disk usage utility for web servers written in PHP

WebDirStat is disk usage utility for web servers, it’s a single PHP file that gives you statistics about disk usage inside a specific Directory ordered by size, in a form of a simple tree table.

Oct 14, 2021
Telegram API made for php (Simple usage)

Telegram Telegram API made for php (Simple usage) How to use? Download the project & place Telegram folder tp your project directory (For example i se

Apr 4, 2022
Composer Plugin for automatically including files for easing function usage in php.

Php Inc Php inc is a composer plugin for automatically including certain files into composer's autoload and autoload-dev files config. Given a set of

Jan 11, 2022
An utility component for XML usage and best practices in PHP

An utility component for XML usage and best practices in PHP

Mar 26, 2022
Learning Management System made in vanilla PHP to learn core concepts and usage of some basic utils

Learning Management System Learning Management System made in vanilla PHP to learn core concepts and usage of some basic utils. Report Bug · Request F

Mar 30, 2022
Provides database storage and retrieval of application settings, with a fallback to the config classes.

Provides database storage and retrieval of application settings, with a fallback to the config classes.

May 17, 2022
This component provides a collection of functions/classes using the symfony/intl package when the Intl extension is not installed.

Symfony Polyfill / Intl: ICU This package provides fallback implementations when the Intl extension is not installed. It is limited to the "en" locale

May 21, 2022
Provides a clean and simple way to configure the WordPress-bundled PHPMailer library, allowing you to quickly get started sending mail through a local or cloud based service of your choice

WP PHPMailer provides a clean and simple way to configure the WordPress-bundled PHPMailer library, allowing you to quickly get started sending mail through a local or cloud based service of your choice.

May 2, 2022
Adds a specific header to every response to disable Google's usage of your site in it's FLoC tracking method.

Go Unfloc Yourself Description A bundle for Symfony 5 that adds a Permissions-Policy header in all the responses to prevent the use of new Google's "F

Feb 25, 2022
Adds a header to every response to try and twart Google's usage of your site in it's FLoC tracking method.

Laravel No FLoC This package will add the Permissions-Policy: interest-cohort=() to try and twart Google's usage of your site in it's FLoC tracking me

Jul 28, 2021