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
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2015-11-10