承接 ajf/newtype 相关项目开发

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

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

ajf/newtype

Composer 安装命令:

composer require ajf/newtype

包简介

A handy function for creating opaque types

关键字:

README 文档

README

newType() is a handy function for creating opaque types, that is, a type that merely wraps another type. It takes two parameters: the name of your new wrapper type, and the name of the type you want to wrap. You get a class with a constructor taking a value of the wrapped type, and an ->unbox() method to get out the value of the wrapped type.

Require it with composer require ajf/newtype to use it. It's PHP 7-only, since PHP 7 is the first version of PHP with scalar type declarations. It would be possible to backport this, though.

An example of where you might use this:

<?php

namespace JaneBlogges\WonderfulApp;

use function ajf\newType\newType;

// Makes the new opaque type!
newType(FilePath::class, 'string');

function moveFile(FilePath $sourcePath, FilePath $destinationPath): bool {
    return rename($sourcePath->unbox(), $destinationPath->unbox());
}

moveFile(new FilePath('foo'), new FilePath('bar'));

Use it for that extra bit of type safety!

If you're after a mere alias that doesn't require explicit conversion to and from, check out PHP's built-in class\_alias function - though beware that only works with classes and not primitive types.

The name comes from Haskell's newtype declaration, which does the same thing:

newtype FilePath = FilePath String

It's similar to Hack's newtype declaration as well, although that works a little differently.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2015-11-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固