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


so if need pass data around application internally there nice predictable way pass around.

i want able @ function , know whats in collection getting passed function.

an array unpredictable data structure, never know you're getting.

i feel it's better have object defined named properties , pass around application instead of array, because way theres sort of definition available.

the issue there bunch of objects accumulating in folder somewhere.

was wondering anyone's opinion on this, , other alternatives?

you might having larger (although - hidden) problem. structure in code should not cross more 1 layer boundary.

if structure (doesn't matter whether array or object) crossing 2 layers, then, unless intentional compromise, sign, there might "architectural flaws" indicating fragility of codebase. such cross-cutting data structures become fault-lines across witch codebase exhibits none-trivial bugs.

if have structures, cross 3 or more layer boundaries, codebase fucked. becomes 1 of "kill fire, before lays eggs" projects.

the solution use this:

instead of having dedicated "data structures" being passed around, focus business logic around domain objects. create @ point in layer, using logic-related behaviour , inject or return other layer affect it.

kinda 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 being passed in out of method not "data structure", formed logical entity, contains specific, business-relate behaviour.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -