sunnyflail/object-creator

A simple class for building objects

1.1.0 2021-08-03 23:18 UTC

This package is auto-updated.

Last update: 2024-05-04 05:10:25 UTC


README

A simple class providing object oriented interface over class invoking

Usage

First you need to import it by requiring composer autoloader AND then importing it in class that will use it

use SunnyFlail\ObjectCreator\ObjectCreator;

Creating object

First you need a common object creator instance and then invoke ObjectCreator::create passing fully qualified class name as an argument (the namespaced CANNOT contain leading backslashes). This returns a mutable copy of ObjectCreator

$creator = new ObjectCreator();
$concreteCreator = $creator->create(Entity::class);

Setting properties

To set properties just invoke ObjectCreator::withProperty on initialised copy, passing name of the property as first value, and value as second. This returns the copy so you can chain it.

$concreteCreator->withProperty("propertyName", "value");

Getting object

To get the object invoke ObjectCreator::getObject on initalised copy.

$object = $concreteCreator->getObject();

Extending

You can make your custom implementation of this by implementing interface IObjectCreator.