71823

how to call global functions classes from namespace PHP

Question:

Is there any way how to avoid to using so often backslash ?

Now if i'm using namespace and calling something global inside it i have to use backslash:

namespace foo; $a = new \my\name(); // instantiates "my\name" class echo \strlen('hi'); // calls function "strlen" $a = \INI_ALL; // $a is set to the value of constant "INI_ALL"

in that case code inside namespace become really ugly, is there any way how to avoid that situation ???

The example was taken from that url: <a href="http://www.php.net/manual/en/language.namespaces.faq.php" rel="nofollow">http://www.php.net/manual/en/language.namespaces.faq.php</a>

But my problem in that, if I need call some built in class from namespace i have to use backslash in front of the name, can I somehow avoid it ?

Answer1:

constants and functions from the global namespace do not have to be prepended with a backslash. PHP will fallback to the global namespace for those on it's own. There is a whole chapter in the PHP manual explaining this:

<ul><li><a href="http://php.net/manual/en/language.namespaces.fallback.php" rel="nofollow">Using namespaces: fallback to global function/constant</a></li> </ul><blockquote>

Inside a namespace, when PHP encounters a unqualified Name in a class name, function or constant context, it resolves these with different priorities. Class names always resolve to the current namespace name. […] For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist.

</blockquote>

Recommend

  • sscanf: get first and last token in a string
  • Code to disable magnifier, set wallpaper, and save image in Google Chrome?
  • HSQL + Hibernate Exception: Wrong column type: Found: double, expected: float
  • PHP & cURL proxy - how to make multi-user cookie jar?
  • How to write string.Contains(someText) in expression Tree
  • What do I do with this error when I run tests in rails?
  • Compress a file with GZipStream while maintaining its meta-data
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • How can I include If-None-Match header in HttpRequestMessage
  • How to pass a value from ASP.NET MVC controller to ASP.NET webforms control inside MVC View?
  • NUnit 3.0 TestCase const custom object arguments
  • Suppressing passwd when calling sqlplus from shell script
  • Security issues with PHP's Readfile method
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Parse a date string in a specific locale (not timezone!)
  • Android activity accessing service's static reference before the service is ready
  • Time complexity of a program which involves multiple variables
  • MVC3 Razor - ListBox pre-select not working
  • Switching to Release Build causes runtime error in Web Reference
  • Display issues when we change from one jquery mobile page to another in firefox
  • JavaScriptCore crash on iOS9
  • How to make Safari send if-modified-since header?
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • How to pass list parameters for each object using Spring MVC?
  • SVN: Merging two branches together
  • Circular dependency while pushing http interceptor
  • bootstrap to use multiple ng-app
  • Transpose CSV data with awk (pivot transformation)
  • How to get icons for entities from eclipse?
  • AngularJs get employee from factory
  • Acquiring multiple attributes from .xml file in c#
  • How to set the response of a form post action to a iframe source?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • How can I remove ASP.NET Designer.cs files?
  • JaxB to read class hierarchy
  • java string with new operator and a literal