2013-07-09 3 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(); 
?> 

아무 것도 출력하지 않았습니다.

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; 
} 

을 그리고 자식 클래스 :
내가 별도로 기능을 시도하고 당신이 반환을 호출하기 전에 출력 버퍼의 결과를 지역화하지 않는 것처럼

+0

'new $ google' =>'new NULL' => 치명적 오류 ...'$'를 버리십시오. – Wrikken

+0

시도해보십시오.'$ google = new google ('http://www.google.com/'); ' –

+0

나는 그것을 떨어 뜨 렸지만 여전히 아무것도 가지고 있지 않다. –

답변

1

이 보이는 잘 간다,이 시도

<?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'에서 부모에게 전화를 걸었지만 결과를 반환하지는 않습니다. 나만의 경우 Google에 특정한 맞춤 로직을 수행 할 계획이 아니라면 아이의 버전을 삭제합니다. 나는 코드를 업데이트했다. –

+0

이 문제는 curl_grab_page가 자식 클래스 u에서 출력을 반환하지 않는 것이지만 부모 클래스에서 반환되기 때문에 출력을 반환해야합니다. 감사 –

관련 문제