PHP Global space

PHPServer Side ProgrammingProgramming

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__ . "\n";
?>

this will print empty string

when this file is included in another namespace

Example

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

Output

This will print following output

testspace
raja
Published on 18-Sep-2020 12:53:08
Advertisements