arshidkv12/traitify

Traitify is a native PHP extension that provides commonly used traits

Installs: 3

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 0

Forks: 1

Open Issues: 1

Language:C

Type:php-ext

v1.0.0 2025-08-09 05:32 UTC

This package is auto-updated.

Last update: 2025-08-10 10:59:33 UTC


README

Traitify is a native PHP extension that provides commonly used traits such as Singleton, implemented in C for optimal performance and better memory efficiency.

🧩 Features

  • ✅ Native Traitify\Singleton trait
  • ✅ Automatic singleton management (getInstance())
  • ✅ Proper support for __construct()
  • 🚀 Built for speed — skips PHP overhead
  • 🧠 Extendable for more traits like Macroable, Loggable, etc.

📦 Installation

1. Clone and build

git clone https://github.com/yourname/traitify.git
cd traitify

phpize
./configure --enable-traitify
make
sudo make install

2. Enable in php.ini

extension=traitify.so

✨ Example Usage

Singleton Trait

<?php
use Traitify\Singleton;

class MyService {
    use Traitify\Singleton;

    public function __construct() {
        echo "Constructing...\n";
    }

    public function hello() {
        return "Hello World";
    }
}

$a = MyService::getInstance();
$b = MyService::getInstance();

var_dump($a === $b);   // true
echo $a->hello();      // Hello World

Macroable Trait

<?php

use Traitify\Macroable;

class Tool {
    use Macroable;
}

Tool::macro('greet', fn($name) => "Hello, $name!");
$t = new Tool();
echo $t->greet("Arshid"); // Hello, Arshid