• Date: August 9, 2011
  • Author: Slawek Lukasiewicz
  • Comments: 4 Comments
  • Category: PHP

Throwing exception when type hinting failed

One of the most annoying behavior of PHP type hinting feature is triggering error instead of throwing exception when instance type is invalid.

class Test
	public function doSomething(A $a) { #code}
class B {}
$testInstance = new Test();
$bInstance = new B();

Above code will interrupt script with catchable fatal error.

Catchable fatal error: Argument 1 passed to Test::doSomething() must be an instance of A, instance of B given…[etc]

Luckily, there is solution to bypassing this behavior. We can use custom error handler and throw exception alone. Custom error handler gives us possibility to handle errors.

Catchable fatal error is an error represented by E_RECOVERABLE_ERROR constant, and described by manual as:

Catchable fatal error. It indicates that a probably dangerous error occured, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR.

This means, that it will not interrupt our script, when we handle it.

Using error handler is quite easy. We have to define function and register it using set_error_handler.

function errorHandler($errno, $errstr, $errfile, $errline) {
	if ( $errno === E_RECOVERABLE_ERROR ) { 
		throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
	return false;

After registration we can use try .. catch block to deal with type hinting exceptions.

$testInstance = new Test();
$bInstance = new B();
try {
} catch(ErrorException $e) {}


As you can see above, I used ErrorException class as exception. This is very neat class! In standard case, Exception class will return line and file name connected with place, where exception was thrown. In our example – line and filename are connected with errorHandler function.

On the other hand, ErrorException uses line and file name passed as constructor arguments, so in our example they are connected with doSomething method in Test class. It is much better for debugging process.

Custom Exception class

Of course we can use custom class for incorrect type exception. It can be done by extracting information from error message. Type hinting custom exception can extend some of SPL exceptions.

  • Date: July 14, 2011
  • Author: Slawek Lukasiewicz
  • Comments: No Comments
  • Category: Linux

Bash script: do something when server is responding too slow

Some time ago I had to write script that should perform some actions when server is responding too slow. I had little time for this task, so I wrote simple bash script using cURL, and set is as Cron job.

curl -s URL --max-time 5 > /dev/null 2> /dev/null
if [ $? -eq 28 ]
 #perform some
 # actions

In above script –max-execution option is used. With this option if connection lasts longer than n seconds (in my example 5 seconds) curl stops and return code 28.

  • Date: June 27, 2011
  • Author: Slawek Lukasiewicz
  • Comments: No Comments
  • Category: MySQL

MySQL: custom sort order

Usually when we need to sort query result we use ORDER BY clause with interesting column. In most cases this will be sufficient, but sometimes we need to sort result in custom way. MySQL provides us some capabilities to perform this operation.
Continue Reading →