Type juggling within Sitemagic CMS

As you may know, PHP is a dynamically typed language. This means that a variable can contain any type of data (string, integer, boolean, resource etc.). Also, the value "1" (string) is comparable to 1 (integer) and True (Boolean). This makes it easy to write programs, as the developer does not have to worry about declaring types. But it also has some downsides. Applications may become buggy, since some code may behave in an unexpected way in certain circumstances, if types are not thought into the application design. The following example demonstrates this.
<?php

$str = "My name is David";
$position = strpos($str, "My");

if ($str == false)
    exit(" 'My' not found in string! ");

?>
In the example above, we look for the word "My" in the sentence "My name is David". The strpos(..) function returns the index of the first occurrence found. If the specified search string is not found, false is returned. In this case, strpos returns index 0. Unfortunately 0 is comparable to false, which means, that the error will be thrown to the user, stating that "My" was not found, even though this is incorrect. This is a common error in many PHP applications.
Fortunately the PHP developers introduced strict comparison to address this problem. The code below demonstrates the use of this mechanism.
<?php

if ($str === false)
    exit(" 'My' not found in string! ");

?>
Notice how === is used instead of ==. This way not only values are compared, but also the types. There is still a problem though. Variables may change type during code execution, if we are not careful. Consider the following example.
<?php

$userInfo = "David Northwood, age ";
$userInfo += 29;

?>
In the example above we intended to add the age 29 to the string $userInfo, unfortunately a minor bug has found its way to the source code. The correct way of assigning the value 29 to the string, is like this: $userInfo .= 29. But since PHP is loosely typed, it converts the string to an integer of value 0 and adds the value 29. Thanks to the dynamically typed language, $userInfo now becomes an integer with the value 29, and we might never realize, that we have a bug.
This demonstrates how easy it is to introduce bugs, of which some may be very hard to find, since they only occure in certain circumstances.
Sitemagic introduces a mechanism for checking types, that will help the developer discover these bugs. The Sitemagic team uses the mechanism to check all function arguments, to ensure that developers do not pass variables of incorrect types to the framework. Developers may choose to use the functionality as well. However, be aware that types are only checked when debugging is enabled in config.xml.php, and that type checking comes with a small performance penalty (which is why is is only used when debugging is enabled).
<?php

$var = 123;
printString($var);

function printString($str)
{
    SMTypeCheck::CheckObject(__METHOD__, "string", $string, SMTypeCheckType::$String);
    echo $str;
}

?>
It is as simple as demonstrated above. The CheckObject function will throw an exception, since $var contains an unsupported type, which will terminate the execution of the application. It is also possible to have the function return True/False instead of throwing an exception. To achieve this, simply invoke the function as shown below.
<?php

$result = SMTypeCheck::CheckObject(__METHOD__, "str", $str, SMTypeCheckType::$String, false);

?>
Checking the content of arrays is also possible. Simply use the CheckArray function instead. In the example below, the validation will fail, if the array contains data with a type different from String.
<?php

// Throws exception if array contains data with a type different from String.
SMTypeCheck::CheckArray(__METHOD__, "array", $array, SMTypeCheckType::$String);

// Returns True/False instead of throwing an exception.
$result = SMTypeCheck::CheckArray(__METHOD__, "array", $array, SMTypeCheckType::$String, false);

?>
Finally it is also possible to have both the CheckObject function and CheckArray functions check for instances of classes, which the following example demonstrates.
<?php

SMTypeCheck::CheckObject(__METHOD__, "obj", $obj, "MyClass");

?>