1

I am writing a custom domain-management class which uses various external services, sort of a wrapper class to make them work together.

Where abouts would I put the connection logic in this class? I won't need all the services at once so it doesn't make sense to put it in the constructor, I'm actually thinking some of the methods will be better off as static methods as they don't really relate to each other, the only thing they have in common is the underlying connections.

I am going to have methods along the lines of:

  • registerDomain() (contacts Nominet)
  • updateDomain(),
  • domainAvailable(), (contacts Nominet)
  • registerDNS(), (contacts Amazon & Nominet)
  • updateDNS(),

Should I check for a connection property in each call (and create it if non-existent) or connect in the classes constructor?

1
  • Idea: The common denominator of your methods could be set up in the wrapper class construtor. For the other methods, you could extend the wrapper class or build separate classes and pass them only what they need to work. Commented Jul 20, 2011 at 9:58

1 Answer 1

2

I think it would make sense to create somthing along the lines of this:

class DomainManager {
    public function __construct($domainData) {}
    public function registerDomain() {
        //connect
        //do stuff
    }
    public function updateDomain() {
        //connect
        //do stuff
    }
    public function isAvailable() {
        //connect
        //do stuff
    }
    public function registerDns() {
        //connect
        //do stuff
    }
    public function updateDns() {
        //connect
        //do stuff
    }
    private function connectToNominet() {}
    private function connectToAmazon() {}
}

Then you have a nice object that encapsulates the logic available for a domain:

$domain1 = new DomainManager('example.com', $user, $foo);
$domain->registerDomain();
$domain->registerDns();
Sign up to request clarification or add additional context in comments.

4 Comments

Cool, would you store the connection in a property and something like this when it's needed: if(!$this->nominet) { $this->connectToNominet() } ? Or should connectToNominet return the connection?
Depends on the type of connection, if it is stateless like a regular ajax request it won't be needed. If it is a more permanent connection however, like you would make to a database server it would make sence to store the connection. The question you need to pose yourself is: can I do multiple request to the connection without having to reconnect?
Probably "DomainController" is a better name for the class than Manager.
@Nicky: This is wrong mate. You're defining $domain1 but using $domain ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.