dsmithhayes/abstractmodel

用于创建简单数据模型的库。

v0.0.1 2017-03-13 23:43 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:18:15 UTC


README

问题:为特定属性编写 getset 方法可能会让人感到疲惫,尤其是当除了获取和设置之外没有其他逻辑时。《AbstractModel》通过允许您构建数据模型并自动生成修改器来解决此问题。

AbstractModel 类实现了 PSR-11 容器接口。

安装

$ composer require dsmithhayes/abstractmodel

用法

<?php

use Dsh\AbstractModel;

/**
 * By default all public values are treated as such.
 */
class User extends AbstractModel
{
    /**
     * Protected properties get mutators by default
     */
    protected $username;
    
    /**
     * Private properties do not.
     */
    private $password;
    
    public function __construct($username, $password)
    {
        $this->username = $username;
        $this->password = $password;
    }
}

// `init()` actually reads the properties and builds a store
// for the values
$user = (new User('dave', 'password'))->init();

echo $user->getUsername(); // yields 'dave'
echo $user->getPassword(); // throws an Exception

$user->init(User::USE_ALL);

echo $user->getPassword(); // yields 'password'