So I was reviewing the source code from PHPWord and I have looked all over and can't figure out how this snippet of code works.
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();
...
}
}
So according to my understanding of php the only way to access the $this->_xmlWriter methods is to call it like this:
$testClass= new PHPWord_Shared_XMLWriter();
$testClass->_xmlWriter->startDocument();
However, in this piece of code from theDocProps.php this line of code is implemented:
$objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY);
$objWriter->startDocument('1.0','UTF-8','yes');
How does this work? I can't replicate it, but it works when I use it in the file. TO BE CLEAR, there is no method defined by PHPWord_Shared_XMLWriter that is called startDocument(). I feel like I'm missing something really simple but I can't even search right for it.
Thanks!
You need to look at the entire definition of the class PHPWord_Shared_XMLWriter
(source here). It uses the __call
magic method to pass method calls to _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!
}
}
By using call_user_func_array
on $this->_xmlWriter
, it causes all inaccessible or undefined methods to be passed to the _xmlWriter
property.
From the docs:
__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