2012-08-31 3 views
1

클래스가 ClassA이고 simple_html_dom의 함수를이 클래스에 사용하고 싶습니다. 어떻게해야합니까? 이 simple_html_dom 클래스 http://www.megafileupload.com/en/file/366382/simple-html-dom-rar.html클래스에서 simple_html_dom의 함수 인스턴스를 사용하는 방법

+0

'클래스 A {공공 $의 DOM의 링크입니다; function __construct() {$ this-> dom = 새 DOMDocument; }}' – Yang

+0

고마워,하지만 아래에 내 코멘트에 게시 된 것들을 할 필요가 –

답변

3
<?php 
    Class A 
    { 
     private $simpleHTML; 

     function __construct() 
     { 
      $this->simpleHTML = new simple_html_dom(); 
      //now you can call all simple html functions using $this->simpleHTML->.. 
     } 

     //define file_get_html as a class method. You can call this as 
     // $x = new A(); 
     //$x->file_get_html..(externally) or $this->file_get_html(.. (internally) 
     function file_get_html($url, 
         $use_include_path = false, 
         $context=null, $offset = -1, 
         $maxLen=-1, $lowercase = true, 
         $forceTagsClosed=true, 
         $target_charset = DEFAULT_TARGET_CHARSET, 
         $stripRN=true, 
         $defaultBRText=DEFAULT_BR_TEXT) 
     { 
      // We DO force the tags to be terminated. 
      $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $defaultBRText); 
      // For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done. 
      $contents = file_get_contents($url, $use_include_path, $context, $offset); 
      // Paperg - use our own mechanism for getting the contents as we want to control the timeout. 
      //$contents = retrieve_url_contents($url); 
      if (empty($contents)) 
      { 
       return false; 
      } 
      // The second parameter can force the selectors to all be lowercase. 
      $dom->load($contents, $lowercase, $stripRN); 
      return $dom; 
     } 
    } 
?> 
+0

간단한 HTML DOM은'function file_get_html()'이 클래스 simple_html_dom을 ouside 가지고있다. 나는 그것을 부르지 않아! 'simple html dom'은 PHP에 대한 커스텀 라이브러리입니다. –

+0

정확하게 이해할 수 있을지 모르겠지만 라이브러리가 클래스로 정의되어 있다면 클래스 A의 생성자 내부에서 인스턴스화 된 클래스의 객체를 가질 수 있습니다. 그것은 클래스 권리의 일원으로서? – raidenace

+0

업데이트 된 질문에 simple_html_dom 파일을 게시했습니다. 그것을 확인하시기 바랍니다! 도와 주셔서 감사합니다! : D –

관련 문제