<?php
/**
* Overload-Class
*
* Use overloading in PHP.
*
* Overload the functions in your PHP-Script approximately as you know it from
* JAVA and other languages. You just need multiple (protected!) functions in
* your class with the special signature to work with them in an object as known
* from JAVA etc.
*
* There are 7 types that you can vary:
* B - Boolean
* I - Integer / Long
* F - Float / Double
* S - String
* A - Array
* O - Object
* R - Ressource
*
* Just add these letters after an underline character ("_") to define the
* signature of the function. All functions have to be protected!
* Here is an example:
*
class Example extends Overload
{
protected function foo()
{
return NULL;
}
protected function foo_S($string)
{
echo $string;
}
protected function foo_SI($string, $integer)
{
echo
$string . " equals " . $integer . ".";
}
}
* Now you can call just "foo" with different signatures:
$o = new Example();
$o->foo() // Will call foo()
$o->foo("Hello World!"); // Will call foo_S("Hello World!")
$o->foo("Pi", 3); // Will call foo_SI("Pi", 3)
$o->foo(3, "Pi"); // Will lead to an error because there is no
method with foo_IS
$o->foo($o, 1); // Will lead to an error because there is no
method with foo_OI
*
* NOTE: It only works in objects / non-static functions.
*
* LICENSE: This source file is subject to
* "Attribution-Noncommercial-No Derivative Works 3.0 Unported" license
* that is available through the world-wide-web at the following URI:
* http://creativecommons.org/licenses/by-nc-nd/3.0/
* If you did not receive a copy of the license and are unable to
* obtain it through the web, please send a note to hide@address.com
* so I can mail you a copy.
* Put this always visible and reachable on your page:
* Overloading 1.1.0 (c) Pretzlaw
* Or if you use more than one script you can enumerate :
* Overloading 1.1.0 , Template 2.0.1 (c) Pretzlaw
*
* 2009/03/24 - Ralf Mike Pretzlaw
* @category Template
* @author Ralf Mike Pretzlaw
* @copyright Ralf Mike Pretzlaw
* @license Attribution-Noncommercial-Share Alike 3.0 Unported
* @link http://creativecommons.org/licenses/by-nc-sa/3.0/
* @version 1.1.0
*
*/
class Overload
{
/**
* Magic-Function __call
*
* @param string $name
* @param array $args
* @return mixed
*/
private function __call($name, $args)
{
$func = "";
// Look for arguments
if (count($args) != 0)
{
$func = "_";
/*
* Walk each argument and check the data-type
*/
foreach ($args as $x)
{
// Switch between scalar and other
if (is_scalar($x))
{
// B - Boolean
if (is_bool($x))
{
$func .= "B";
continue;
}
// I - Integer / Long
if (is_int($x))
{
$func .= "I";
continue;
}
// S - String
if (is_string($x))
{
$func .= "S";
continue;
}
// F - Float / Double
if (is_float($x))
{
$func .= "F";
continue;
}
}
else
{
// A - Array
if (is_array($x))
{
$func .= "A";
continue;
}
// O - Object
if (is_object($x))
{
$func .= "O";
continue;
}
// R - Ressource
if (is_resource($x))
{
$func .= "R";
continue;
}
}
}
}
// Check if function with signature exists
if (method_exists($this, $name.$func))
{
// exists: Call it
return call_user_func_array(array($this, $name.$func), $args);
}
else
{
// not found: lookup functions with optional arguments
for ($i = strlen($func); $i > 0; $i--)
{
$func = substr($func, 0, $i);
if (method_exists($this, $name.$func))
{
$i = true;
break;
}
}
// check if it exists with the new signature
if ($i)
{
// found with new signature: call it
return call_user_func_array(array($this, $name.$func), $args);
}
else
{
// not found: show error
// get line etc. where it was called
$a = debug_backtrace();
$i = count($a) - 1;
// error
trigger_error(
"Function '" . $name .
"' not found or wrong types of arguments (" .
$a[$i]["file"] . ":" . $a[$i]["line"] . ")"
, E_USER_WARNING);
}
}
}
}
?>