dongye9191 2014-03-08 15:36
浏览 38
已采纳

PHP MVC - 当标签字符串随对象类型变化时,标签字符串是否成为模型的一部分而不是视图?

I have a "summery" view for an Iterated list of polymorphic objects, and I experimenting with a clean separation of the view and the model.

So, where do I put the static label information? I am trying to avoid this sort of thing in the view...

while loop do
  switch (prop->type)
    case foo:
      echo "label1: ". prop->data1;
      echo "label2: ". prop->data2;
    break;
    case bar:
      echo "label3: ". prop->data1;
      echo "label2: ". prop->data2;
      ...

This can make the view code pretty messy, especially when you have up to 10 different prop->type and the summery has to be shown in several different areas.

I guess the real question here is...has the label ceased to be part of the view and now is part of the model because it changes dynamically depending on the object type?

  • 写回答

1条回答 默认 最新

  • doushe7934 2014-03-08 16:36
    关注

    Using a switch statement isn't probably the best practice here. Everytime you need a new type you would have to add a new case. It would be better to store the corresponding labels along with the data in your class in my opinion. But let's tackle the problem.

    Decorator

    One thing you can do is to implement a Decorator for your prop class that adds a functionality to get the corresponding Label.

    class PrintLabelPropDecorator {
    
        protected $prop;
    
        public function __construct(Prop $prop)
        {
            $this->prop = $prop;
        }
    
        public function printLabels()
        {
            switch($this->prop->type)
            {
                case 'foo':
                    echo 'Label1: ' . $this->prop->data1;
                    echo 'Label2: ' . $this->prop->data2;
                break; 
    
                //[...]
            }
        }
    
    }
    

    The first thing we do is to inject the class we want to decorate into the constructor. Then we write the actual decoration method printLabels(). That does the actual check and prints the label information.

    Now we only need to decorate the actual class. For simplification reasons I do this in a pseudo-controller method. You should outsource it by your demands.

    <?php
    public function myControllerMethod()
    {
        //$props = whatever you need to do to get the prop objects
    
        $printLabelPropDecorators = [];
    
        // That should usually better not be handled by the controller
        foreach($props AS $prop)
        {
            $printLabelPropDecorators[] = new PrintLabelPropDecorator($prop);
        }
    
        $this->view->create('myfancyview', compact('printLabelPropDecorators'));
    
    }
    

    We create an array that will hold our decorated objects and then iterate trough the base objects and create our decorator objects. Then we simply pass the array with our decorators to the view. (The decoration process is not quite elegant, you could for example write some kind of Collection for this).

    Now the only thing left to do is the view. It should be self explanatory:

    <?php foreach($printLabelPropDecorators AS $prop): ?>
    
        <?php $prop->printLabels(); ?>
    
    <?php endforeach ?>
    

    That could be one approach to do it. The actual logic that decides which labels to print does now live in the Decorator and you are not cluttering your view.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路