Preparing for the ZEND-Exam, I found a question where a class redefined the strlen-function:
namespace MyFramework\MyString
{
function strlen ($str)
{
return \strlen($str) * 2; // return double string-length
}
}
I never came across that "\function"-thing before and could not get an explanation from my trainer - can somebody pls. help shed some light...?
Introduced in PHP 5.3, \
is a namespace separator.
In your example, the outer strlen is MyFramework\MyString\strlen, the inner strlen is the global one.
Without any namespace definition, all class and function definitions are placed into the global space - as it was in PHP before namespaces were supported. Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace.
Reference: http://www.php.net/manual/en/language.namespaces.global.php