簡體   English   中英

有關使用php中另一個類的存儲類方法的說明

[英]Explanation about using a method of a stored class from another class in php

因此,我正在查看PHPWord的源代碼,並且四處張望,無法弄清楚這段代碼的工作方式。

class PHPWord_Shared_XMLWriter {

/**
 * Internal XMLWriter
 *
 * @var XMLWriter
 */
private $_xmlWriter;

public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = './') {
    // Create internal XMLWriter
    $this->_xmlWriter = new XMLWriter();
    ...
}
}

因此,根據我對php的理解,訪問$ this-> _ xmlWriter方法的唯一方法是這樣調用:

$testClass= new PHPWord_Shared_XMLWriter();
$testClass->_xmlWriter->startDocument();

但是,在來自TheDocProps.php的這段代碼中,實現了以下代碼行:

$objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY);
$objWriter->startDocument('1.0','UTF-8','yes');

這是如何運作的? 我無法復制它,但是當我在文件中使用它時它可以工作。 顯然,PHPWord_Shared_XMLWriter定義的方法沒有稱為startDocument()的方法。 我感覺好像缺少了一些非常簡單的東西,但是我什至找不到合適的東西。

謝謝!

您需要查看PHPWord_Shared_XMLWriter的整個定義在此處提供源代碼 )。 它使用__call magic方法將方法調用傳遞給_xmlWriter

 /**
  * Catch function calls (and pass them to internal XMLWriter)
  *
  * @param unknown_type $function
  * @param unknown_type $args
  */
 public function __call($function, $args)
 {
     try {
         @call_user_func_array(array($this->_xmlWriter, $function), $args);
     } catch (Exception $ex) {
         // Do nothing!
     }
 }

通過在$this->_xmlWriter上使用call_user_func_array ,它將使所有無法訪問或未定義的方法傳遞給_xmlWriter屬性。

從文檔:

__call( unknown_type $function, unknown_type $args )
    Catch function calls (and pass them to internal XMLWriter)
    Parameters
        $function
            unknown_type
            $function
        $args
            unknown_type
            $args

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM