PHP: Static functions in a Class

Static functions can be used without instantiating the object.

A quick example of a static function is shown below as well as a comparison with a non static function.

A quick example of this (in PHP5):

class TestClass{
	static function staticFunction(){
		echo "static function called";
	}
 
	function nonStaticFunction(){
		echo "nonstatic function was called";
	}
}
 
TestClass::staticFunction(); //will echo static function called
 
$testClass = new TestClass();
$testClass.nonStaticFunction(); //will echo nonstatic function was called
unset($testClass);

PHP: converting a uuid stored as BINARY(16) in mysql to RFC 4122 format

If you are storing uuid’s are BINARY(16) in the database, then the following will turn the stored id to the “original” format.

$uuidReadable = unpack("h*",$uuid);
$uuidReadable = preg_replace("/([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})/", "$1-$2-$3-$4-$5", $uuidReadable);
$uuidReadable = array_merge($uuidReadable);
echo "uuid is " . $uuidReadable[0];

PHP5: Quick on timing a script

Sometimes it is nice to know how long a scrip took to execute (or a part of a script).
This is a simple way to time a script in php.

$time_start = microtime(true);
 
usleep(500);
 
$time_end = microtime(true);
$time = $time_end - $time_start;
 
echo "run time was " . $time . " seconds";

SimpleXML tags with – in the name

Sometimes a xml file will have tags with “-“s in the name.
It is not to hard to parse this using simplexml, just use the tagname inside curly brackets like: “{tag-name}”

<?xml version="1.0" encoding="utf-8"?>
<medprod xmlns:npl="urn:schemas-instance" version="129">
  <statuses>
    <mpa_status xmlns:mpa="urn:schemas-mpa" datastatus="valid" procstatus="reviewed"/>
  </statuses>
  <mpa_pharmaceutical-form-group-lx xmlns:mpa="urn:schemas-mpa" v="OSD"/>
</medprod>

Let’s assume we load the above in a SimpleXML object called $simplemedprodXml

$simplemedprodXml= simplexml_load_string($xmlString);
echo (string)$simplemedprodXml['version'];
echo (string)$simplemedprodXml->statuses->mpa_status['datastatus'];
echo (string)$simplemedprodXml->statuses->mpa_status['procstatus'];
echo (string)$simplemedprodXml->{'mpa_pharmaceutical-form-group-lx'}['v'];

PHP: casting stdClass Object to a array

When using Soapclient to access a webservice the returned object will be of a stdClass Object.

If you don’t wish to access this as an object, but would find it easier to use it as an array then a quick cast solves this.

/*As an object*/
print_r($soapResult->contents);
/*cast to an array*/
$soapResult = (array)$soapResult;
print_r($soapResult['contents']);

PHP5: Quick on exceptions

Very quick (&dirty):

1
2
3
4
5
6
7
8
try{
	doStuff();
}
catch (Exception $e){
	echo 'Error on line '. $e->getLine().' in '. $e->getFile() . $e->getMessage();
	//Or if you are working inside an object, you could use.
	echo 'Error on line '.$this->getLine().' in '.$this->getFile() . $e->getMessage();
}

Now to catch a specific kind of exception (in this case CustomException), and say woups, but to re-throw all other Exceptions.

1
2
3
4
5
6
7
8
9
10
11
try{
	doStuffThatCanGiveCustomException();
}
catch (Exception $e){
	//echo get_class($e) . "<br/>";
	if (get_class($e) == "CustomException" && $e->getMessage() === "My custom exception message."){
		echo "woups";
	}
	else
		throw $e;
}

To throw a custom exception:

 throw new Exception('My exception message');

For more informaion about this please check out the php manual