Like this?
<?php
namespace sample
{
class Sample_class_1
{
public function test_func_1($text)
{
echo $text;
}
}
class Sample_class_2
{
public static function test_func_2()
{
$c = new Sample_class_1();
$c->test_func_1("func 2<br />");
}
public static function test_func_3()
{
$c = new Sample_class_1();
$c->test_func_1("func 3<br />");
}
}
}
namespace
{
// Directly addressing a class
$c = new sample\Sample_class_1();
$c->test_func_1("Hello world<br />");
sample\Sample_class_2::test_func_2();
use sample\Sample_class_2;
sample\Sample_class_2::test_func_3();
}
namespace sample2
{
// Directly addressing a class
$c = new sample\Sample_class_1();
$c->test_func_1("Hello world<br />");
sample\Sample_class_2::test_func_2();
use sample\Sample_class_2;
sample\Sample_class_2::test_func_3();
}
If you are in another file, you do not need to call namespace {to enter the root namespace. So imagine the following code: "ns2.php", and the source code was in "ns1.php":
include("ns1.php");
$c = new sample\Sample_class_1();
$c->test_func_1("Hello world<br />");
sample\Sample_class_2::test_func_2();
use sample\Sample_class_2;
sample\Sample_class_2::test_func_3();
source
share