Location: PHPKode > scripts > Java and C# compatible scalar type hinting > typehint.php
<?php

/**
 * Simple type hinting (part of Lotos Framework)
 *
 * Copyright (c) 2005-2010 Artur Graniszewski (hide@address.com) 
 * All rights reserved.
 * 
 * @category   Library
 * @package    Lotos
 * @subpackage QoS
 * @copyright  Copyright (c) 2005-2010 Artur Graniszewski (hide@address.com)
 * @license    GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007
 * @version    $Id$
 */
 
define('TYPEHINT_MAX_HEAP_SIZE', -100);

if(!defined("E_DEPRECATED")) {
	define("E_DEPRECATED", 8192);
	define("E_USER_DEPRECATED", 16384);
}

/**
 * Main class.
 */
class TypeHint
{
    /**
     * Callback to the original error handler (if not default)
     * 
     * @var callback
     */
    public static $originalErrorHandler;
    
    /**
     * New error handler
     * 
     * @param int $errorLevel
     * @param string $errorMessage
     * @return bool True if not error, false otherwise
     */
    public function onError($errorLevel, $errorMessage) {
    	// Local hints cache
		static $hintOk = array();
		if(
			isset($hintOk[$errorMessage]) 
		) {
			return true;
		} 
		
		if(preg_match('/^Argument \d+ passed to (?<namespace>(([a-zA-Z_]{1}[a-zA-Z0-9_]+)\\\)+)?[:a-zA-Z0-9_]+\(\) must be an instance of (?<hintName>[a-zA-Z0-9_\\\]+), (?<typeName>[a-z]+) given/AUD', $errorMessage, $matches)) {
			
			$hintOk = array_slice($hintOk, TYPEHINT_MAX_HEAP_SIZE);
			$matches["typeName"] = (isset($matches["namespace"]) ? $matches["namespace"] : "").str_replace(array("boolean", "integer"), array("bool", "int"), $matches["typeName"]);
			return $matches["hintName"] === $matches["typeName"] ? $hintOk[$errorMessage] = true : (isset(self::$originalErrorHandler) ? call_user_func_array(self::$originalErrorHandler, func_get_args()) : false);
		}

		return isset(self::$originalErrorHandler) ? call_user_func_array(self::$originalErrorHandler, func_get_args()) : false;	
    }
    
    /**
     * Main constructor.
     * 
     * @return TypeHint
     */
    public function __construct() {
    	// For performance reasons we are only interested in the E_RECOVERABLE_ERROR 
        self::$originalErrorHandler = set_error_handler(array($this, 'onError') , E_RECOVERABLE_ERROR^E_NOTICE^E_WARNING^E_USER_ERROR^E_USER_NOTICE^E_USER_WARNING^E_USER_DEPRECATED^E_DEPRECATED^E_STRICT);
    } 
}    

Return current item: Java and C# compatible scalar type hinting