php实现hack中的Shape特性

用php进行静态类型编程,估计是我的一个心结。

依次有几篇文章都记录了我的一些探索:

从PHP 7 开始,PHP支持函数和方法的参数及返回值的标量类型标注,逐渐走出了这一步。

但数据传递的过程中,基础的标量类型很多时候是不够用的。比如参数太多的时候,一般用数组或对象的方式传递,二者中用得最多是数组 - 万能的数组,但数组的结构不明确,在数据传递的过程中,对于DTO(Data Transfer Object,数据传输对象)相应数据结构的理解和验证,往往需要额外花费不少时间和精力。在我看来,Hack 语言的Shape类型很好的解决了这个问题。

hack中Shape的用法如下:

type customer = shape('id' => int, 'name' => string);

function create_user(int $id, string $name): customer {
  return shape('id' => $id, 'name' => $name);
}

function ts_shape(): void {
  $c = create_user(0, "James");
  var_dump($c['id']);
  var_dump($c['name']);
}

ts_shape();
Output
int(0)
string(5) "James"

今天动手实现类似hack中的Shape定义,hack中shape本质上是一个数组。

Shapes 是一种特殊的别名类型,代表结构固化的数组 - 固定的键名和类型。定义的Shape可用于类型标注场合。

借助于php的gettype函数,实现类似强类型的概念,不做类型casting。

支持php的基本类型:int、bool、float、string,及array、object、null。

基本用法:

class Customer extends Shape
{
    public function __construct(array $data) {
        parent::__construct(
            ['id' => self::int, 'name' => self::string, 'categories' => self::array],
            $data
        );
    }
}

//数据访问与数组一样,只是一言不合就会抛异常,确保在开发阶段,做好数据类型分析和转换

$customer = new Customer(['id' => 102, 'name' => 'jimmy', 'categories' => [10, 21, 22]]);//如果categories写成categories2,meta中定义的categories就会被填充一个对应的默认值。

$customer['id'] = 103; //如果传'103'就会抛异常
var_dump($customer['id']);
var_dump($customer['categories']);
echo count($customer);
var_dump($customer->toArray());//PHP的array强转还不支持魔术方法定制toArray行为,只能由用户自行调用了

完整代码的查看地址:https://github.com/web3d/lang/blob/master/src/AppserverIo/Lang/Shape.php

原文地址:https://www.cnblogs.com/x3d/p/php-shape.html