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

Overview

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.

Comments
  • Minimum PHP version change in minor is breaking dependents

    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. ❤️

    invalid 
    opened by PeeHaa 21
  • Can't instantiate final class extending ArrayIterator

    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

    enhancement 
    opened by ciaranmcnulty 18
  • HHVM fails to clone DateTime [Failing test only]

    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.

    davem@wes: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
    davem@wes: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:

    davem@wes:instantiator:failing-hhvm-test$ cat t.php
    <?php
    
    $a = new DateTime();
    $b = clone $a;
    
    echo $b->format('r');
    davem@wes:instantiator:failing-hhvm-test$ hhvm t.php
    Mon, 06 Oct 2014 23:28:45 +0100%
    
    bug wontfix 
    opened by davedevelopment 15
  • version 1.1.0 broke phpunit on PHP 5.4 ~ PHP 5.6

    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.

    invalid 
    opened by kinosang 7
  • Unable to instanciate Symfony\Component\HttpFoundation\File\File

    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 ?

    bug invalid 
    opened by jjsaunier 7
  • Instantiator attempts to clone classes that are not cloneable

    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...

    bug 
    opened by davedevelopment 6
  • Cannot instantiate final class if it extends internal

    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

    bug 
    opened by ciaranmcnulty 5
  • Adds support of internal child class instantiation

    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

    enhancement 
    opened by gquemener 4
  • doctrine/instantiator 1.0.1 requires php ~5.3 -> your PHP version (7.1.8) does not satisfy that

    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.

    bug duplicate 
    opened by shakaran 4
  • Dependency problem

    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.

    question 
    opened by Soviann 4
  • Avoid cloning uncloneables

    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
    
    bug 
    opened by davedevelopment 3
  • Promoted property causing Error

    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.

    opened by garak 9
  • Typehint className in instantiate method

    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 ?

    enhancement BC break 
    opened by Valouleloup 3
  • What about to add the capability to set properties with given set of values?

    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?

    enhancement 
    opened by lorenzomar 9
  • [WIP] #9 - adding phpunit test that checks instantiator against all internal classes

    [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.

    enhancement 
    opened by Ocramius 1
Releases(1.4.1)
Owner
Doctrine
The Doctrine Project is the home to several PHP libraries primarily focused on database storage and object mapping.
Doctrine
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

Ralph Schindler 59 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

Steve McDougall 10 Aug 18, 2022
The Enobrev\ORM library is a small framework of classes meant to be used for simply mapping a mysql database to PHP classes, and for creating simply SQL statements using those classes.

The Enobrev\ORM library is a small framework of classes meant to be used for simply mapping a mysql database to PHP classes, and for creating simply SQL statements using those classes.

Mark Armendariz 0 Jan 7, 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

PHPStan 247 Jan 2, 2023
Validate your input data in a simple way, an easy way and right way. no framework required. For simple or large. project.

wepesi_validation this module will help to do your own input validation from http request POST or GET. INTEGRATION The integration is the simple thing

Boss 4 Dec 17, 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

PHPOffice 192 Dec 17, 2022
This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube features.

Laravel Youtube Client This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube featu

Tilson Mateus 6 May 31, 2023
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

solid 1 Oct 23, 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

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

null 2 Nov 20, 2021
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

null 3 Feb 14, 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

Creatuity Corp. 64 Dec 5, 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 2.3k Jan 5, 2023
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

Rafael Martin Soto 10 Oct 15, 2022
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.

Yassine 2 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

null 0 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

Krak 5 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

Laminas Project 13 Nov 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

TitansLab 1 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.

CodeIgniter 4 web framework 47 Dec 29, 2022