2015-04-03 54 views
0
Class Html_Pdf_Export { 
    var $first_name; 
    var $last_name; 
    //alot of data variables 

    //How I have it now 
    function getHtml() 
    { 
     $html = "<!DOCTYPE html> 
     1000 lines of code with data variables 
     </html>"; 

     $this->html = $html; 
     return $this->html; 
    } 

    function convertToPdf() 
    { 
     //function that converts $this->html to Pdf 
    } 

    //How I want the function to be but How do I pass all the data variables? 
    function loadHtml() 
    { 
     $html_load_template = new Html_Load_Template('the_template_i_want_to_load_with_data_variables'); 
     $this->html = $html_load_template; 
     return $this->html; 
    } 
} 

我有一個將Html轉換爲PDF的類。我在那個類中的HTML膨脹了1000-1500行的HTML代碼,最終轉換爲PDF。爲了減少臃腫,我決定將所有html分離到另一個名爲Html_Load_Template的類。如何將Html_Pdf_Export具有的所有數據變量傳遞給類Html_Load_Template?類功能正在加載顯示爲變數的HTML頁面

謝謝

回答

0

好吧,如果我的理解是否正確,你只需要getHtml()函數轉移到Html_Load_Template類(體力勞動)。因此,它看起來像:

Class Html_Pdf_Export { 
    var $first_name; 
    var $last_name; 
    //alot of data variables 

    function convertToPdf() 
    { 
     //function that converts $this->html to Pdf 
    } 

    //How I want the function to be but How do I pass all the data variables? 
    function loadHtml() 
    { 
     $html_load_template = new Html_Load_Template('the_template_i_want_to_load_with_data_variables'); 
     $this->html = $html_load_template->getHtml(); 
     return $this->html; 
    } 
} 

Class Html_Load_Template { 
    public function getHtml() 
    { 
     $html = "<!DOCTYPE html> 
     1000 lines of code with data variables 
     </html>"; 

     $this->html = $html; 
     return $this->html; 
    } 

    // other functions if needed 
} 
0

我不太清楚你的「Html_Load_Template」級的樣子,但基本上我想你想外包「getHtml」部分一個不同的文件,對吧?

所以,如果你的代碼移動到負載類...

function getHtml() 
{ 
    $html = "<!DOCTYPE html> 
    1000 lines of code with data variables 
    </html>"; 
    $this->html = $html; 
    return $this->html; 
} 

...不會,如果你得到的數據通過工作...

$this->html = $html_load_template->getHtml(); 
+0

哦, AdamM有同樣的想法。 :) – Sunny 2015-04-03 17:40:19