doumouyi4039 2018-05-11 05:29
浏览 59
已采纳

具有许多属性的不可变对象

I've got a class that I want to make immutable but that class has a lot of properties.

<?php

class Gameworld {

    /** @var string */
    private $name;

    /** @var string */
    private $type;

    /** @var bool */
    private $is_online;

    /** @var int */
    private $online_players;

    /** @var int */
    private $online_players_record;

    /** @var string */
    private $description;

    /** @var string */
    private $location;

    /** @var \DateTime */
    private $created_at;

}

How to create such object? When I introduce public function __construct() with all those properties it will get bloated. If I introduce setters it won't be immutable anymore.


EDIT: I was thinking about making setters but ones that could be only used once. Thanks to that I won't have bloated constructor but for some reason it seems not like a good idea. Something like:

class Gameworld {

    ... old properties ...

    /** @var array */
    private $used_setters = [];

    public function setName(string $name){
        if(in_array('name', $this->used_setters)){
            throw new ImmutableException('Class Gameworld is immutable.');
        }

        $this->name = $name;
        $this->used_setters[] = 'name';
    }

}

展开全部

  • 写回答

2条回答 默认 最新

  • douci1677 2018-05-11 06:29
    关注

    I would use __set() magic method to check if the value is set else I would throw an exception:

    class Gameworld {
    
        /** @var string */
        private $name;
    
        /** @var string */
        private $type;
    
        /** @var bool */
        private $is_online;
    
        /** @var int */
        private $online_players;
    
        /** @var int */
        private $online_players_record;
    
        /** @var string */
        private $description;
    
        /** @var string */
        private $location;
    
        /** @var \DateTime */
        private $created_at;
    
        public function __set($property, $value)  
        {  
            if (property_exists($this, $property)) {  
                if(is_null($this->$property)){
                    $this->$property = $value;
                    return $this;
                }
                throw MyCustomException();
            }
            throw UndefinedClassVariableException();
        }
    }
    

    A basic usage would be:

    $x = new Gameworld();
    $x->name = "OK";
    $x->is_online = true;
    $x->name="Exception";
    

    P.S: Exceptions must extend the base exception class or you could trigger or log an error, depends on what you want

    P.S.S: An alternative sollution would be to use __call() method and check if the value is null

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部