drxp993551 2013-04-03 11:37
浏览 66

延迟加载和依赖注入模式中的冗余getter和setter

I'm implementing the lazy initialization and dependency injection pattern in my PHP application at the moment and face the following question:

Every class has a bunch of getter and setter methods providing objects of foreign classes. Is there an elegant way to remove this "redundancy" to a parent class or some kind of factory?

EDIT: without loosing the advantages of testability ;-)

EDIT2: here is an example of a getter and setter method like I use them:

function getSql() {
    if (is_object($this->sql) === FALSE) {
        $registry = \Registry::getInstance();
        $factories = $registry->get('factories');
        $database = $factories['databaseSql'];
        $this->sql = $database->getSql();
    }

    return $this->sql;
}

function setSql($sqlObject) {
    // ... some checks
    $this->sql = $sqlObject;
}

EDIT3 I followed the idea of using traits so here is a sample solution using a trait for the class "Registration":

trait Registration {

    private $registration = null;

    public function getRegistration() {
        $registry = \Registry::getInstance();
        $factories = $registry->get('factories');

        if (is_object($this->registration) === TRUE) {
            // get obect if instance already exists
            $registration = $this->registration;
        } else if (isset($factories['registration']) === TRUE) {
            // get object using the matching factory
            $registrationFactory = $factories['registration'];
            $registration = $registrationFactory->getRegistration();
        } else if (class_exists('\engine\classes\Registration') === TRUE) {
            // get object using the default object class when no specific factory is defined
            $registration = new \engine\classes\Registration();
        } else {
            throw new \Exception('error getting an instance of Registration');
        }

        $this->registration = $registration;

        return $registration;
    }

    public function setRegistration($object) {
        if (is_object($object) === FALSE)
            throw new \Exception('invalid registration object');

        $this->registration = $object;
    }
}

Usage

class Foo {
    use Registration;

    public function bar() {
        $reg = $this->getRegistration();
        // ... use instance of Registration
    }
}
  • 写回答

1条回答 默认 最新

  • dqwh0109 2013-04-05 12:51
    关注

    Do you know OOP advantages of PHP language?

    Inheritance

    Traits

    Magic getter and setters

    May be some Aspect-Oriented Programming. For example Implementing Reusable Fluent Interface Pattern in PHP With AOP.

    It's so many links because you did not provide any code for example.

    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上