I'm making a simple httpcode monitor as an example as I'm trying to teach myself object orientated PHP. I have written the code below but would be interested if it can be improved. In particular I'm not sure this line:

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

This seems not very efficent 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 it's url? That might be a dumb questions 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 someway of it remembering it's url which I passed two lines above?

Any other code improvements would be appreciated as well. Thanks in advance.

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);
share|improve this question

1 Answer

up vote 5 down vote accepted

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();
    
share|improve this answer
Thank you that is exactly what I couldn't figure out with get_httpcode() method. That's really helped, cheers. – Ben Paton Aug 27 '12 at 8:22

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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