dongzhuang2030 2013-11-04 17:28
浏览 32
已采纳

类结构和传递数据

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.

  • 写回答

1条回答 默认 最新

  • dousi4257 2013-11-04 17:37
    关注

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

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

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line