Skip to content

Guide · Open source

How to use doctrine/instantiator: create PHP objects without a constructor

Updated · written with AI from the project's own documentation and checked against our catalogue data

Doctrine Instantiator is a small PHP library that creates an instance of a class without running its constructor or using any of the class's own methods. This guide shows how to install it with Composer, call it in code, and decide whether it fits your project. It is aimed at developers who build tools such as ORMs or serializers, or who write tests for classes that are hard to construct.

Doctrine Instantiatordoctrine/instantiator · ★ 11k · MIT

Quick start

  1. 1Check prerequisites. Confirm that PHP and Composer are installed on your machine.
  2. 2Install the package. Run composer require doctrine/instantiator in your project directory.
  3. 3Create an Instantiator. Instantiate the Doctrine\Instantiator\Instantiator class with new.
  4. 4Call instantiate. Pass the fully qualified class name of the class you want to the instantiate method.
  5. 5Set required state. Assign any properties the constructor would normally have set, since it will not run.
  6. 6Test the result. Check that the returned object has the state your code expects before using it.

What Doctrine Instantiator does

The library provides a way of avoiding constructors when instantiating PHP classes. The README says it is the approach Doctrine and similar tools use to build objects without constructor logic.

Because the constructor is skipped, any setup that normally happens there does not run. Your code is responsible for setting the object's state afterwards.

Install Doctrine Instantiator with Composer

You need PHP and Composer, PHP's standard package manager. The suggested installation method is Composer, run from your project directory.

composer require doctrine/instantiator

Create an object without calling the constructor

Create an Instantiator object, then pass the fully qualified class name to its instantiate method. The result is a new instance of that class, created without running the constructor or any of the class's API.

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

Know the risks before you use it

Skipping the constructor can leave properties uninitialized. Code that assumes the constructor has run may then fail when it reads those properties.

Use the library where that trade-off is intended, such as rebuilding objects from stored data or creating test doubles for classes with side effects or hard-to-satisfy dependencies. For normal object creation, call the constructor as usual.

  • Fits: ORMs, serializers, tools that rebuild objects from stored data
  • Fits: tests for classes whose constructor has side effects or hard dependencies
  • Does not fit: everyday application code that needs a fully set-up object
  • Does not fit: non-PHP projects, since it only works with PHP classes

Migrating from ocramius/instantiator

The library was migrated from ocramius/instantiator, which has been donated to the Doctrine organization and is now deprecated in favour of this package. If your project uses the older package, the README points to doctrine/instantiator as its replacement.

Contribute or get help

The README asks people who want to help to read CONTRIBUTING.md in the repository before opening contributions.

What people use it for

  • Rebuilding entities from stored data in an ORM or data mapper
  • Serializing and deserializing objects in a custom serializer
  • Creating an instance of a class in a unit test when its constructor has side effects
  • Building objects whose constructor requires dependencies that are hard to supply in a test

Frequently asked questions

Is Doctrine Instantiator free to use?

It is released under the MIT licence, as listed in the project's details. Check the licence file in the repository for the full terms before you use it in a commercial project.

What do I need to use Doctrine Instantiator?

You need PHP and Composer. The README's suggested installation method is composer require doctrine/instantiator.

How do I install doctrine/instantiator?

Run composer require doctrine/instantiator in your project directory. The README lists this as the suggested method.

Does Doctrine Instantiator call the constructor?

No. Its purpose is to create instances without running the constructor or using the class's own methods. This means properties set in the constructor may be left uninitialized.

Can I use it with languages other than PHP?

No. The library only works with PHP classes, so it is not suitable for non-PHP projects.

What is the alternative to doctrine/instantiator?

The project's comparable package is ocramius/instantiator. The README says that package was migrated to Doctrine and is now deprecated in favour of doctrine/instantiator.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Doctrine Instantiator suits PHP maintainers building ORMs, serializers or tests that need objects without running their constructors. If you only need normal object creation, or your project is not written in PHP, use standard instantiation instead.

Install options & downloads