定制 agorlov/php-constructor-overloading 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

agorlov/php-constructor-overloading

Composer 安装命令:

composer require agorlov/php-constructor-overloading

包简介

Multiple constructors in PHP

README 文档

README

Constructor overload PHP methods, like in Java or C++

Based on article (how to adopt this technic from Java to PHP):

https://www.yegor256.com/2015/05/28/one-primary-constructor.html

For PHP > 7

How to install

$ composer require agorlov/php-constructor-overloading

Usage example

use AG\OverloadedConstructor;


final class Cash {
  /** @var int */
  private $cents; 
  /** @var string */
  private $currency;
  
  private function constrEmpty() { // secondary
    $this->__construct(0);
  }
  
  private function constrCnts(int $cts) { // secondary
    $this->__construct($cts, "USD");
  }
  
  private function constrPrimary(int $cts, string $crn) { // primary
    $this->cents = $cts;
    $this->currency = $crn;
  }
  
  public function __construct() {
    // overloaded constructors invocation
    $method = (new OverloadedConstructor($this, func_get_args()))->constructor();
    $this->$method(...func_get_args());  
  }
  
  // methods here
}

var_dump(new Cash());

more examples: in example.php and example2.php

Valid scalar hints

  • array
  • callable
  • bool not boolean!
  • float not double!
  • int not integer!
  • string
  • iterable
  • object

Tests

$ composer install
$ vendor/bin/phpunit OverloadedConstructorTest.php

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固