Reputation: 5931
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.
Upvotes: 1
Views: 81
Reputation: 884
You can try abstract class and static property to share it with all your instance
Upvotes: 1