简体   繁体   中英

Is it allowed to create an object of the same class inside a function?

I am new to OOP. I am just learning and I have to use it to find the actual/final URL of a link that redirects.

Class ABC {
    public function getWebPage($url, $redirectcallback = null){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");

    $html = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($http_code == 301 || $http_code == 302) {
        list($httpheader) = explode("\r\n\r\n", $html, 2);
        $matches = array();
        preg_match('/(Location:|URI:)(.*?)\n/', $httpheader, $matches);
        $nurl = trim(array_pop($matches));
        $url_parsed = parse_url($nurl);
        if (isset($url_parsed)) {
            if($redirectcallback){ // callback
                 $redirectcallback($nurl, $url);
            }
            $html = $this->getWebPage($nurl, $redirectcallback);
        }
    }
    return $html;
}

}

The above function inside class call the same function again and again to find the actual url. However I am already calling the class in some other file

$obj = new ABC;
$url = "http://www.anrdoezrs.net/asd/?ak=123";
$someurl = $obj->getWebPage($url);

But this does not work. Please suggest.

Class ABC {
   public function xyz($url){
      $html = $this->xyz($url);
      return $html;
     }
}

In another class call it with

$obj = new ABC;
$url = "Some value";
$someurl = $obj->xyz($url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM