承接 tasoft/operation-overload 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tasoft/operation-overload

最新稳定版本:v0.8.1

Composer 安装命令:

composer require tasoft/operation-overload

包简介

README 文档

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

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-19