John Smith
John Smith

Reputation: 6207

If two object refers to each other, is that a sign of bad code?

given this code:

class EmailHander
{
    public $mailer;

    public function __construct()
    {
        $mailer = new PHPMailer();
    }

    public function getEmail ($name)
    {
        return new EmailTemplate ($name, $this);
    }
}

class EmailTemplate
{
    public $body, $subject, $handler;

    public __construct ($name, EmailHander $handler)
    {
        require ($name.'.html');
        $this->handler = $handler;
    }

    public function send ($target)
    {
        $this->handler->mailer->send ($target, ......);
    }
}

there is an EmailHandler which deals with the mailer, and many EmailTemplates which represents the emails. So,

$handler = new EmailHandler();
$email = $hander->getEmail ('subScribe');
$email->send('[email protected]');

but this is might bad. The EmailHandler produces an EmailTemplate. But EmailTemplate refers to EmailHandler, so this is a circular thing, is that good or bad?

Upvotes: 1

Views: 55

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385325

There is no circular dependency in this code. There is a one-way dependency from an email to its handler; doesn't matter that the handler set up the dependency.

Upvotes: 2

Related Questions