How to use global variable in PHP

Namespace provide the way to group related classes.

Example 1: create testClass.php in folder 'some/name'

            <?php
            namespace some\name;

            class testClass {

                function say(){
                    echo("Hello");
                }
            }
            ?>
            

Create test file named 'test.php' with the same level of folder 'some'

            <?php
            spl_autoload_register(function ($class_name) {
                include $class_name . '.php';
            });

            use some\name\testClass;

            $a = new testClass();
            $a->say();
            ?>
            

It can also help in using the same class name on the same system (with different namespace)

Example 2: create testClass.php in folder 'other/name'

            <?php
            namespace other\name;

            class testClass {

                function say(){
                    echo("It's me");
                }
            }
            ?>
            

Modify 'test.php' as following

            <?php
            spl_autoload_register(function ($class_name) {
                include $class_name . '.php';
            });

            use some\name\testClass as testClass1;
            use other\name\testClass as testClass2;

            $a = new testClass1();
            $a->say();

            echo("<br />");

            $b = new testClass2();
            $b->say();
            ?>