Search code examples
phpoopdata-structuresarchitecturesystem

How to handle passing data around internally within the app, in an PHP application?


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?


Solution

  • 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.