I need check if called function belongs to the current namespace or it is defined globally.
<?php
namespace Test;
function is_string($str) { return false; }
var_dump(is_string('test')); // will be always false, is the namespaced is_string()
var_dump(\is_string('test')); // will be the global is_string()
Now I need check if is_string()
or \is_string()
is defined inside the namespace or is global, and it will occur at the same file. Sometimes the namespaced version will not be defined.
I have tried to use the ReflectionFunction
class, but it no works:
var_dump((new \ReflectionFunction('is_string'))->inNamespace()); // returns false, instead of true
var_dump((new \ReflectionFunction('is_string'))->getNamespaceName()); // returns '', instead of 'Test'
var_dump((new \ReflectionFunction('\\is_string'))->inNamespace()); // returns true, that is right
var_dump((new \ReflectionFunction('\\is_string'))->getNamespaceName()); // returns '', that is right
Seems that it always points to the global, but when I just run is_string()
it uses the namespaced version (that is correct on this context).
Aucun commentaire:
Enregistrer un commentaire