2013-07-09 38 views
0

即時嘗試在抽象類有一個很好的做法方法,所以我創建了一個簡單的捲曲包裝類,但不幸的是它不工作。
抽象使用抽象類的簡單捲曲包裝方法?

<?php 
abstract class curl{ 
    private $url; 

    public function __construct($url){ 
     $this->url = $url ; 
    } 
    public function curl_grab_page() 
    { 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
      curl_setopt($ch, CURLOPT_URL, $this->url); 
      curl_setopt($ch, CURLOPT_HEADER, TRUE); 
      curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
      ob_start(); // prevent any output 
      return curl_exec ($ch); // execute the curl command 
      ob_end_clean(); // stop preventing output 
      curl_close ($ch); 
    } 
    public abstract function getHTML(); 

} 
?> 

孩子

<?php 
class google extends curl{ 
    private $url; 
    function __construct($url) { 
     parent::__construct($url); 
    } 
    function curl_grab_page(){ 
     parent::curl_grab_page(); 
    } 
    function getHTML(){ 
     return $this->curl_grab_page(); 
    } 
} 

,這是我在頭版怎麼稱呼。

<?php 
include 'classes/class.curl.php'; 
include 'classes/class.google.php'; 
$google = new google('http://www.google.com/'); 
echo $google->getHTML(); 
?> 

它沒有打印出任何東西。
我單獨審判的功能和它去細

+0

'new $ google' =>'new NULL' =>致命錯誤...丟棄'$'。 – Wrikken

+0

嘗試:'$ google = new google('http://www.google.com/');' –

+0

我確實沒有放棄它,但仍然沒有任何 –

回答

1

看起來你是不是調用返回之前本地化輸出緩衝區的結果,試試這個:

public function curl_grab_page() 
{ 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
     curl_setopt($ch, CURLOPT_URL, $this->url); 
     curl_setopt($ch, CURLOPT_HEADER, TRUE); 
     curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 

     // Don't need any output buffering b/c you set CURLOPT_RETURNTRANSFER to true, which means the results will be returned via curl_exec 
     //ob_start(); // prevent any output 
     //return curl_exec ($ch); // execute the curl command 
     //ob_end_clean(); // stop preventing output 

     $contents = curl_exec($ch); 

     curl_close ($ch); 

     return $contents; 
} 

而子類:

<?php 
class google extends curl{ 
    // Don't need this, if you set the parent's scope to protected which means child class has access 
    // private $url; 

    function __construct($url) { 
     parent::__construct($url); 
    } 

    // Don't really need this method unless you plan on doing some custom logic   
    function curl_grab_page(){ 

     // If you keep this method I added a return here so we can get the results of the call 
     return parent::curl_grab_page(); 
    } 

    function getHTML(){ 
     return $this->curl_grab_page(); 
    } 
} 
+0

它沒有打印任何內容。 我認爲我錯過的問題是類的層次和錯過抽象方法的使用,因爲我嘗試了功能分開出類,它進行得很好,並打印輸出 –

+0

好吧,幾個問題。您將CURLOPT_RETURNTRANSFER設置爲true,但您試圖使用緩衝區捕獲輸出,您只需要一個或另一個。此外,在孩子的'curl_grab_page'中,您正在調用父級,但不返回結果。如果是我,我只是刪除孩子的版本,除非你打算做一些針對谷歌的自定義邏輯。我更新了代碼。 –

+0

這是curl_grab_page沒有在子類中返回輸出的問題,儘管它必須返回輸出,因爲它在父類中返回。謝謝 –