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.

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

报告相同问题?

悬赏问题

  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置