PHP Global space


Introduction

In absence of any namespace definition, all definitions of class, function etc. are placed in a global namespace. If a name is prefixed with \ , it will mean that the name is required from the global space even in the context of the namespace.

Using global space specification

Example

<?
namespace test;
/* This function istest\fopen */
function fopen() {
   /* ... */
   $f = \fopen(...); // call global fopen
   return $f;
}
?>

Included files will default to the global namespace.

Example

#test1.php
<?php
echo __NAMESPACE__ . "
"; ?>

this will print empty string

when this file is included in another namespace

Example

#test2.php
<?php
namespace testspace {
   include 'test1.php';
   echo __NAMESPACE__ . "
"; } ?>

Output

This will print following output

testspace

Updated on: 18-Sep-2020

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements