PHP raw POST data

To read the raw data being posted to a php page (for instance when $_POST is empty due to problems with encoding types or $HTTP_RAW_POST_DATA not being readable due to php.ini settings) reading file_get_contents is a nice solution.

$postData = file_get_contents('php://input');

SimpleXml save formated output

When using the SimpleXml->asXML(‘file.xml’) the output is simply written onto one line.
like

<?xml version="1.0" encoding="UTF-8"?>
<product><companyId>1</companyId><productId>1:1</productId></product>

There is nothing wrong with this but if you add line breaks and indentations the xml file looks better and is easier to (manually) read.
Unfortunately there is no way to do this using SimpleXML, but there is a quick and dirty way to do this; and that is to import the SimpleXMLobject to a DOMElement and do it there so some example code

$xmlDom = dom_import_simplexml($simpleXmlObject);
$xmlDom->formatOutput = true;
$xmlDom->save("test.xml");

This would result in an xml file looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<product>
	<companyId>1</companyId>
	<productId>1:1</productId>
</product>

Easier to read but takes some extra space on the disk (might not be much but it is good to remember).

PHP5: Quick on calling a parents constructor

Using OOP there comes a need to call the constructor of a parent class, this is not hard to do

class TestParent {
    public function __construct() {
        var_dump('blah');
    }
}
 
class TestChild extends TestParent {
    public function __construct() {
        parent::__construct();
    }
}
 
$a = new TestChild(); //Output will be: string 'blah' (length=4)

In PHP4 this would have looked like (this still works in PHP5)

class TestParent {
    public function TestParent() {
        var_dump('blah');
    }
}
 
class TestChild extends TestParent {
    public function TestChild() {
        parent::TestParent();
    }
}
$a = new TestChild(); //Output will be: string 'blah' (length=4)