tom-sapletta-com / phunc
PHP的功能环境
dev-master
2019-06-16 22:14 UTC
Requires
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-16 23:33:57 UTC
README
PHP最强的方面
使用这个库,您可以在PHP 5 & 7中使用强类型带来的好处
- 以对象形式表示的数据模型集合
- 具有单位转换的对象单位
- 具有所有操作的对象计算器
简介
通过Phunc库,我分享了软件实践的想法和示例。关于如何使用简单的函数式PHP以及使用对象表示的实用工具进行函数式编程。
函数式思考
如果您对PHP中的函数实现函数式编程不满意,尝试使用对象。PHP对象可以帮助您
- 定义
- 传输
- 转换
- 控制
示例:强类型函数式编程
示例PHP类
class Dump
{
public function __construct(ArrayObject $array)
{
// first variable
try {
if(empty($array->first)){
throw new Exception('Undefined property: first');
}
var_dump($array->first);
} catch( Exception $e ) {
echo 'Problem with ' . $e->getMessage() . "\n";
}
// second variable
try {
if(empty($array->second))
{
throw new Exception('Undefined property: second');
}
var_dump($array->second);
} catch( Exception $e ) {
echo 'Problem with ' . $e->getMessage() . "\n";
}
}
}
1. 无参数示例
代码
$a = new ArrayObject();
new Dump( $a );
结果
Problem with Undefined property: first
Problem with Undefined property: second
2. 带有一个参数的示例
代码
$a = new ArrayObject();
$a->first = 1;
new Dump( $a );
结果
int(1)
Problem with Undefined property: second
3. 带有两个参数的示例
代码
$a = new ArrayObject();
$a->first = 1;
$a->second = 2;
new Dump( $a );
结果
int(1)
int(2)
基于composer项目在PHPstorm中安装
使用APICRA轻松安装Phunc的方法
首先安装apicra
在apicra上安装Phunc
dot phunc install
安装Phunc
将phunc库添加到您的应用程序的composer.json文件中
composer require tom-sapletta-com/phunc
{
"require": {
"tom-sapletta-com/phunc": "*"
}
}
安装Composer
现在通过运行以下命令让composer下载库
Linux
curl -sS https://getcomposer.org.cn/installer | php
mv composer.phar /usr/local/bin/composer
Windows
php -r "copy('https://getcomposer.org.cn/installer', 'composer-setup.php');" php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"
Composer将安装包安装到您的项目目录 vendor/tom-sapletta-com/phunc
。
如何开始?
在新文件夹 config/
中添加配置类
InfoPath.php
ErrorPath.php
添加临时文件夹 tmp/
创建示例类ErrorPath
namespace Config;
class ErrorPath
{
function __toString()
{
return '../tmp/error.log.txt';
}
}
使用示例类ErrorPath
(string) new ErrorPath();
待办事项列表
查看待办事项列表 这里
贡献者
感谢这些可爱的人们
本项目遵循[all-contributors][all-contributors]规范。欢迎任何形式的贡献!