Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
/ makeable Public archive

Static constructor trait and interface

License

Notifications You must be signed in to change notification settings

danielsdeboer/makeable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Latest Stable Version License

Overview

This package provides a static constructor interface, and a trait that implements the interface.

Installation

Via Composer:

composer require aviator/makeable

Testing

Via Composer:

composer test

Usage

Use the trait:

class Something
{
    use MakeableTrait;
}

Then the class can be instantiated using Class::make(...$args):

$instance = Something::make($arg1, $arg2);

The interface is optional, though it can be useful in composite interfaces to specify that a static constructor should be present:

interface SomeInterface extends Makeable, SomeOtherInterface
{
    /* ... etc */
}

Since the static constructor simply returns new static(...$args), it can be used in abstract classes and parent classes without having to re-use it on child classes:

abstract class Seuss
{
    use MakeableTrait;
}

class ThingOne extends Seuss {};
class ThingTwo extends ThingOne {};

// Get an instance of ThingOne
$instance = ThingOne::make();

// Get an instance of ThingTwo
$instance = ThingTwo::make();

Other

License

This package is licensed with the MIT License (MIT).