Is it possible to write a PHP function in a XML
<doThis>
doThis(){
bang bang bang!
}
</doThis>
and use simplexml_load_file
to get the object with that function?
$foo = simplexml_load_file($xmlAbove);
$foo->doThis(); //bang bang bang!
Is it possible to write a PHP function in a XML
<doThis>
doThis(){
bang bang bang!
}
</doThis>
and use simplexml_load_file
to get the object with that function?
$foo = simplexml_load_file($xmlAbove);
$foo->doThis(); //bang bang bang!
Firstly, you should always consider the security implications of running "code" like this. If this XML file has remote origins, and you execute the code, then it might do anything. What about exec(rm -fR /some/important/path);
? Or a database call that deletes everything? There needs to be a very strong trust relationship between the sender and your code, and also strong security protocols to prevent man-in-the-middle and other attacks.
To your question, it is not possible directly. As commented, XML is data, not code, and is supposed to be portable, meaning your PHP code will be nonsense to another client accepting this XML.
However, you could use eval
. Maybe like this:
eval($foo->doThis);
Note: I wouldn't recommend this at all. It isn't the intended use of XML and it has many problems. Please, think of some other way.