1

Below I have a series of classes. I am trying to figure out the best way to pass my data from Process.php into each of the following classes.

/CLI/
    /Process/
        /Condition
            Rule.php
        Condition.php
        Single.php
    Process.php
    Response.php
    Registry.php <-- I don't want to use this, It feels sloppy

At first I wanted to pass data in the hierarchy like below (Im not extending anything at the moment favoring Composition over Inheritance):

\CLI\Process.php

public function run() {
    $condition = new Condition($dataToShare);
}

\CLI\Process\Condition.php

public function __construct($dataToShare) {
    $this->data = $dataToShare;
}

\CLI\Process\Condition\Rule.php

public function __construct($dataToShare) {
    $this->data = $dataToShare;
}

Instead I decided to use a Registry Pattern because I'm paranoid. I feel like the shared data going from class to class and it's messy. Then I found myself setting some Registry values inside a subclass, and it was just as bad!

So a co-worker told me to try an Abstract class so each of these guys can extend it. My problem is that if I extend an Abstract Class then my values won't be set (Because it's a new parent instance).

Is my best option a Singleton? Does anyone have any suggestions to keep this cleaner? I want this to be clean, and I dont want to have this paranoid thought of contaminating data over continually passing it down levels.

While I ask this question it feels like Im speaking Chinese underwater.

JREAM
  • 5,741
  • 11
  • 46
  • 84
  • 1
    See if http://stackoverflow.com/questions/6094744/dependency-hell-how-does-one-pass-dependencies-to-deeply-nested-objects/6095002#6095002 answers your question. [And no, a Singleton is never your best option.](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323) – Gordon Nov 04 '13 at 17:40
  • Love his first note "It's a common misconception that dependencies need to be passed through the object graph." – JREAM Nov 04 '13 at 17:48
  • @Gordon With a house builder example, how would you suggest I fetch the builder in the subclasses? I can't re-instantiate it or else It'll reset values that would be set in it. – JREAM Nov 04 '13 at 17:53
  • 1
    why would you? Just assemble everything up front in your bootstrap and then use it. The only reason to pass the builder into the collaborator graph is when you need to create an object graph later at runtime. Another option would be to use a DI container. See http://stackoverflow.com/questions/17142644 and http://stackoverflow.com/questions/9348376 – Gordon Nov 04 '13 at 18:00

1 Answers1

1

You can try abstract class and static property to share it with all your instance

SmasherHell
  • 894
  • 8
  • 19