clippings/freezable

冻结对象中的值

0.3.0 2020-02-11 10:33 UTC

This package is auto-updated.

Last update: 2024-09-22 06:32:15 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Latest Unstable Version Total Downloads License

冻结对象中的值

需要 PHP 5.4 或更高版本。

用法

<?php

use Clippings\Freezable\FreezableTrait;

class Item {

    use FreezableTrait;

    private $value = NULL;

    public function performFreeze()
    {
        $this->value = $this->computeValue();
    }

    public function performUnfreeze()
    {
        $this->value = NULL;
    }

    private function computeValue()
    {
        // computation from external sources, database, other objects etc.
        return pi() * pi();
    }

    public function getValue()
    {
        return $this->isFrozen() ? $this->value : $this->computeValue();
    }
}

许可协议

版权所有 (c) 2014,Clippings Ltd。由 Ivan Kerin & Haralan Dobrev 开发

根据 BSD-3-Clause 许可协议,请阅读 LICENSE 文件。