dsgs8208 2011-04-01 22:26
浏览 22
已采纳

更好地管理功能之间的“消息”?

I my framework I have some functions that when done they can add some messages to a queue for reporting.

Example: I have a function that takes a path of a photo and

  • if the image is not .jpg it converts it to .jpg
  • if the image is greater than 500kB it reduces its size

I have a global $msgQueue=array(); and whenever all the logic of the page is done, in my template I echo to the users all the reports (that the functions could add during the exectuion).

In that case 2 messages would be added to $msgQueue:

  • The image was PNG and has been converted to JPG
  • The image was 2000x1000 and now is 1000x500

But this kind of behaviour I think it's not standard. If I want share with someone one of my function (in this case is checkImage($path)) It can't work because the functions needs that global array to put their report msgs.

Is there a standard approach to solve this so I can share my functions with someone else and don't worry about this dependence?

  • 写回答

3条回答 默认 最新

  • douhuang1852 2011-04-01 22:37
    关注

    My suggestion would be to use a class, something like:

    class Report(){
        public $msgQueue;
    
        function addReport($string){ 
             array_push($this->msgQueue, $string);  //store new report
        }
    
        function showReports(){
             //loop over the message queue
             ...
        }
    }
    

    Benefits:

    1. You could have different kind of reports using the same class, separating process from errors for example, $processes = new Report and $errors = new Report

    2. No need to declare vars as globals, the class preserves the value of its property $msgQueue you would only need to $processes->addReport("Resizing image to XXX")

    3. The benefits of OOP, having a logical structure, etc.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog