tasoft/operation-overload

v0.8.1 2023-03-20 08:26 UTC

This package is auto-updated.

Last update: 2024-04-20 10:56:38 UTC


README

Did you ever want to add, subtract or multiply objects?
This is a rfc on https://wiki.php.net/rfc/userspace_operator_overloading

I've created an extension that enables this functionality.

Installation

$ cd ~
$ git clone https://github.com/tasoftch/php-extension-operation-overload.git
$ cd php-extension-operation-overload/ext
$ phpize
$ ./configure --enable-tasoft_usr_op_overload
$ make
$ sudo make install

This compiles the source on your machine.
Next find the php.ini file

$ php --ini

Will list scanned ini files.
Add the following line to that php.ini file: extension=tasoft_usr_op_overload

<?php
var_dump( extension_loaded('tasoft_usr_op_overload') ); // Should be true

Usage

This package ships with a header class

<?php
use TASoft\Util\OperationOverloadingObject;

All of its child classes may implement static method to the common operations in PHP.
If the extension is not installed, the class exists anyway, but it will trigger a warning and the operation overloading won't work.

There are PHP interfaces declared which describe the implemented operation overloading.

The objects don't need to implement the interfaces. If the requested method exists, they get called otherwise the zend engine produces a fatal error.

Example

<?php
use TASoft\Util\OperationOverloadingObject as OpOv;

class Number extends OpOv {
    private $number;
    public function __construct($number) {
        $this->number = $number;
    }
    
    public static function __add($op1,$op2){
        if($op1 instanceof Number)
            $op1 = $op1->number;
        if($op2 instanceof Number)
            $op2 = $op2->number;
        
        return $op1 + $op2;
    }
}

$n1 = new Number(23);
$n2 = new Number(18);

echo $n1 + $n2; // 41