arunagirinathar/php-design-patterns

A lightweight PHP package implementing common design patterns like Singleton and Factory. Useful for clean and efficient object management in PHP projects.

v1.3.1 2025-04-23 06:25 UTC

This package is auto-updated.

Last update: 2025-05-23 06:43:47 UTC


README

A lightweight PHP package implementing common design patterns like Singleton and Factory. Useful for clean and efficient object management in PHP projects.

Design Patterns (PHP)

License: GPL v3

A lightweight PHP package implementing common design patterns like Singleton and Factory. Useful for clean and efficient object management in PHP projects.

📌 Features

Singleton – Ensures only one instance of a class exists.
Factory – Creates objects without specifying the exact class type.

Polyfills (Extra Feature)

Older versions of PHP may not have certain function available. For example json_validate() is not available in PHP versions older than 8.3.0. This class transparently implements all this functionality as a shim / polyfill without disturbing environments where such functions are available.

Functions currently implemented in the polyfills

json_validate

📥 Installation

Using Composer:

composer require arunagirinathar/design-patterns

🛠 Usage

Singleton Example

use Arunagirinathar\DesignPatterns\Singleton;

class MySingleton extends Singleton
{
    private string $data = "Hello, Singleton!";

    public function getData(): string
    {
        return $this->data;
    }
}

$instance1 = MySingleton::getInstance();
$instance2 = MySingleton::getInstance();

echo $instance1->getData(); // Hello, Singleton!
var_dump($instance1 === $instance2); // true (Both refer to the same instance)

📝 License

This project is licensed under the GNU GPLv3. See the LICENSE file for details.

📬 Contact

Maintainer: Arunagirinathar
📧 Email: arunagirinathar@gmail.com
🔗 GitHub: Arunagirinathar/DesignPatterns