简体   繁体   English

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

[英]Better management of “messages” between function?

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 如果图像不是.jpg,则将其转换为.jpg
  • if the image is greater than 500kB it reduces its size 如果图像大于500kB,则会减小其大小

I have a global $msgQueue=array(); 我有一个全局$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: 在这种情况下,会将2条消息添加到$ msgQueue中:

  • The image was PNG and has been converted to JPG 图片为PNG,已转换为JPG
  • The image was 2000x1000 and now is 1000x500 图片为2000x1000,现在为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. 如果我想与某人共享我的函数之一(在这种情况下为checkImage($path) ),则该函数将无法正常工作,因为函数需要该全局数组来放置其报告消息。

Is there a standard approach to solve this so I can share my functions with someone else and don't worry about this dependence? 是否有解决此问题的标准方法,以便我可以与他人共享我的功能,而不必担心这种依赖性?

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 您可以使用同一类创建不同类型的报告,将过程与错误分开,例如, $processes = new Report$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") 无需将vars声明为全局变量,该类保留其属性$msgQueue的值,您只需要$processes->addReport("Resizing image to XXX")

  3. The benefits of OOP, having a logical structure, etc. OOP的好处,具有逻辑结构等

I don't really think there is a standard approach. 我真的不认为有一种标准的方法。 What I do is this: 我的工作是这样的:

  • Give each library (Image, File, etc..) its own message array. 给每个库(图像,文件等)自己的消息数组。

  • Use a Message library that has its own message array as well as an array of "sources" that can be built/added to via Message::addSource($class, $propertyName) . 使用具有自己的消息数组以及可以通过Message::addSource($class, $propertyName)构建/添加的“源”数组的消息库。 (I add a message source right after I make an instance of a library, such as Image). (我在创建库实例(例如Image)后立即添加了消息源)。

    • When Message::render() is executed, it combines each source's messages with the Message class's own message then renders them. 执行Message :: render()时,它将每个源的消息与Message类自己的消息组合在一起,然后呈现它们。

Doing it that way allows each library to be independent of others while still being able have both a per-instance and global message array. 这样,每个库都可以独立于其他库,同时仍然可以具有每个实例和全局消息数组。

Is there a standard approach to solve this? 是否有解决此问题的标准方法?

Yes, its called OOP :) (see amosrivera 's answer for that). 是的,它称为OOP :)(有关此信息,请参见amosrivera的答案)。

But if OOP is not an option (but you should seriously consider it nonetheless), then refactoring the functions to accept a messageQueue argument (and passing it by reference) might work. 但是,如果OOP不是一个选项(但是您仍然应该认真考虑),则可以重构函数以接受messageQueue参数(并通过引用传递它)。

// passing $messageQueue by reference by prepending &
function checkImage( $path, array &$messageQueue = null )
{
    /* do image checking */
    $result = /* the result of the image checking */;

    // if $messageQueue argument is provided
    if( $messageQueue !== null )
    {
        // add the message to the queue
        $messageQueue[] = 'Image checking done';
    }

    return $result; // false or true perhaps.
}

Usage: 用法:

$messageQueue = array();

if( checkImage( $somePath, $messageQueue ) )
{
    echo 'checking image succeeded';
}
else
{
    echo 'checking image failed';
}

var_dump( $messageQueue );

// would output something like:
Array(1) (
    'Image checking done'
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM