duancaozen6066 2017-08-20 21:07
浏览 92
已采纳

如何在PHP应用程序中处理应用程序内部传递数据?

So if I need to pass data around the application internally is there nice predictable way to pass it around.

I want to be able to look at a function and know whats in the collection that is getting passed into the function.

An array is an unpredictable data structure, so you never know what you're getting.

I feel like it's better to have an object defined with named properties and pass that around the application instead of an array, because that way theres SOME sort of a definition available.

The issue with that is that there will just be an bunch of objects accumulating in some folder somewhere.

Was wondering anyone's opinion on this, and any other alternatives?

  • 写回答

1条回答 默认 最新

  • duannao3402 2017-08-20 21:56
    关注

    You might be having a larger (although - hidden) problem. A structure in your code should not cross more than one layer boundary.

    If some structure (doesn't matter whether it is an array or an object) is crossing two layers, then, unless it is an intentional compromise, it is a sign, that there might be "architectural flaws" indicating fragility of your codebase. Such cross-cutting data structures become the fault-lines across witch your codebase exhibits none-trivial bugs.

    If your have structures, that cross 3 or more layer boundaries, your codebase is fucked. It becomes one of those "kill it with fire, before it lays eggs" projects.

    The solution that I use is this:

    Instead of having dedicated "data structures" being passed around, focus your business logic around domain objects. You create it at some point in the layer, where you will be actually using it logic-related behaviour and inject it or return it to other layer only to affect it.

    Kinda like this:

    public function loginWithPassword(Entity\EmailIdentity $identity, string $password): Entity\CookieIdentity
    {
        if ($identity->matchPassword($password) === false) {
            throw new PasswordMismatch;
        }
        $identity->setPassword($password);
        $this->updateEmailIdentityOnUse($identity);
        $cookie = $this->createCookieIdentity($identity);
    
        return $cookie;
    }
    

    What is being passed in an out of this method is not some "data structure", but a fully formed logical entity, which contains specific, business-relate behaviour.

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

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1