yitznewton/maybe-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

yitznewton/maybe-php

Composer 安装命令:

composer require yitznewton/maybe-php

包简介

A Maybe monad implementation for PHP

README 文档

README

Travis build status PHP 5.3 not supported PHP 5.4 supported PHP 5.5 supported PHP 5.6 supported HHVM tested BSD 2-Clause license

A Maybe monad implementation for PHP

This project was wholly inspired by a blog post by @linepogl.

Motivation

Dealing with null values (and, in PHP, falsy values) is tedious and prone to developer error (viz the null pointer exception, trying to dereference a null).

In my exposure to Haskell, I learned about the awesomeness of pattern matching, whereby you can get the compiler to force yourself to handle all possibilities. This combines with a tool called Maybe to require specific handling for "null" and "non-null" possibilities.

PHP does not offer pattern matching, but we can still use classes to wrap raw values, and require us to handle null conditions, without repeated explicit null checking and conditionals.

Examples

Simple

Before:

$blogpost = $repository->get($blogpostId);
echo $blogpost->teaser();  // oh noe! what if $blogpost is null?! :boom:

After:

$blogpost = new \Yitznewton\Maybe\Maybe($repository->get($blogpostId));
echo $blogpost->select(function ($bp) { $bp->teaser(); })->valueOr('No blogpost found');

With callback

$blogpost = new \Yitznewton\Maybe\Maybe($repository->get($blogpostId));
$callback = function () {
    return someExpensiveOperation();
};
echo $blogpost->select(function ($bp) { $bp->teaser(); })->valueOrCallback($callback);

Loose-falsy

// $process->execute() normally returns a result object, but sometimes returns false
$result = new LooseMaybe($process->execute());

echo $result->select(function ($resultObject) { $resultObject->getStatus(); })->valueOr('failed');
// echoes 'failed' when the result was false

Performance

In a simple test using PHP 5.5, performance was approximately 20% that of a straight is_null() check in an if/else conditional. In other words it takes 5 times as long to run.

You can reproduce the test locally by running the profiling testsuite in PHPUnit. You will first need to install XHProf, and override the XHProf lib directory using a local phpunit.xml config.

$ ./vendor/bin/phpunit --testsuite=profiling

Dictionary wrapper

maybe-php includes an array wrapper called Dictionary, whereby trying to access properties on the wrapper will return a Maybe object. You can specify whether to return a plain Maybe (default) or a LooseMaybe.

$dictionary = new \Yitznewton\Maybe\Dictionary([
    'foo' => 'bar',
]);

$dictionary->foo->valueOr('quux');        // 'bar'
$dictionary->noSuchKey->valueOr('quux');  // 'quux'

// with LooseMaybe

$dictionary = new \Yitznewton\Maybe\Dictionary([
    'foo' => false,
], \Yitznewton\Maybe\LooseMaybe::class);

$dictionary->foo->valueOr('quux'); // 'quux', because loose falsy

yitznewton/maybe-php 适用场景与选型建议

yitznewton/maybe-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 108 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 yitznewton/maybe-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yitznewton/maybe-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 yitznewton/maybe-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2014-11-02