sahil-gulati / overloading
此包的最新版本(1.0.0)没有提供许可信息。
PHP 重载
1.0.0
2016-09-07 06:33 UTC
Requires
- php: >=5.3
This package is auto-updated.
Last update: 2024-09-08 03:31:49 UTC
README
这是一个介绍性框架,用于通过闭包支持 PHP 重载。可以在闭包的帮助下定义具有相同名称的多个函数。每个函数及其实现都会经过不同的验证方法。本文档中提供了完整的基础使用示例。
安装
composer require sahil-gulati/overloading
或者
在您的项目目录中创建 composer.json 文件
{ "require":{ "sahil-gulati/overloading":"1.0.0" } }
composer install
使用方法
<?php include 'vendor/autoload.php'; class OverloadingTest extends Overloading\Overloading { public $x=""; public $y=""; public function __construct() { parent::__construct( func_get_args(), function($x) { $this->x=$x; echo "Setting the value of x"; }, function($x,$y) { $this->x=$x; $this->y=$y; echo "Setting the value of x and y"; } ); } public function declareFunction() { parent::__declare_testing_function(function($x){ $this->x=$x; echo "X will be replaced by new value."; }); parent::__declare_testing_function(function($x,$y){ $this->x=$x; $this->y=$y; echo "X and Y will be replaced by new values"; }); } } try { $obj =new OverloadingTest("s"); $obj->declareFunction(); $obj->testing_function("s","g"); } catch (\Overloading\Exception\Exception $ex) { echo $ex->getErrorMessageForCode(); }
输出
Setting the value of x x and y will be replaced by new values
验证
- 构造函数定义
parent::__construct( func_get_args(), //array of arguments passed in constructor** function($x) { //constructor definition 1.** $this->x=$x; echo "Setting the value of x"; }, function($x,$y) { //constructor definition 2.** $this->x=$x; $this->y=$y; echo "Setting the value of x and y"; } //.., //.., //.. );
- 函数定义
parent::__declare_FUNCTION_NAME(function($x) { //FUNCTION_NAME is name of function to define** echo "In function FUNCTION_NAME"; });
特性
1. 构造函数重载
2. 函数重载
注意
:在这个框架中,函数定义及其调用不依赖于参数类型,而是依赖于参数数量。例如,如果您定义了两个函数,一个有3个参数,另一个也有3个参数,那么第二个函数的定义将覆盖第一个函数的定义,因此如果您调用该函数,则第二个函数/闭包将被执行。这可以看作是一个特性/缺点。此框架的后续实现将有效地处理具有相同数量的参数的类型处理。
免责声明
:此框架不声称支持像其他语言一样解析重载。这只是使您能够使用 PHP 闭包代替重载的实现。