开发者

Structure of PHP class, use static methods or not?

开发者 https://www.devze.com 2023-03-21 09:27 出处:网络
I am writing a custom domain-management class which uses various external services, sort of a wrapper class to make them work开发者_如何学运维 together.

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?


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号