3
\$\begingroup\$

I'm making a simple httpcode monitor as an example for teaching myself PHP OOP. I have written the code below and would be interested in having it improved.

In particular, I'm not sure about this line:

$httpcode = $website->get_httpcode($url);

This seems very inefficient to me as I have to to pass the URL back to the website class right after I have already done just that two lines above. Shouldn't it already remember its URL? That might be a dumb question, but in my mind, I've already made an object called website and given it a URL. Then if I want the httpcode of the URL, then I feel there should be some way for it to remember its URL which I passed two lines above.

class website {
var $url;

function __construct($url) {
    $this->url = $url;
}

public function get_url() {
    return $this->url;
}

public function get_httpcode($url) {
    //get the http status code
    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    $ch=curl_init();
    curl_setopt ($ch, CURLOPT_URL,$url );
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_VERBOSE,false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 3);
    $page=curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $httpcode;
}
}

// create an instance of the website class and pass the url   
$website = new website("http://www.google.com");
$url = $website->get_url();
$httpcode = $website->get_httpcode($url);
\$\endgroup\$
5
\$\begingroup\$

Two things:

  1. Always specify the visibility. Don't use the old var keyword, use public, private or protected. Since the class-member $url contains internal information, you'll probably want to hide that information (you've already implemented a getter method so that the information can be read but not written):

    class website {
        protected $url;
    
        public function __construct($url) {
            $this->url = $url;
        }
    
        // ...
    
  2. Every instance of this class seems to be meant to represent one website. But your method "get_httpcode()" works for every website (it expects a URL; this is exactly what you've already noticed by yourself). It would be more natural to skip the parameter and return the HTTP-Statuscode for the url that is bound to the instance (you've "remembered" the URL in the class-member variable $this->url):

    class website {
        // ...
    
        public function get_httpcode() {
            // ...
            curl_setopt($ch, CURLOPT_URL, $this->url);
            // ...
        }
    }
    
    // Usage:
    $google = new website("http://www.google.com");
    $statusCode = $google->get_httpcode();
    
\$\endgroup\$
  • \$\begingroup\$ Thank you that is exactly what I couldn't figure out with get_httpcode() method. That's really helped, cheers. \$\endgroup\$ – Ben Paton Aug 27 '12 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.