Part 1 - PHP Namespace Basics
By default, PHPMaker uses your project name as the main namespace, for example:
PHPMaker2026\MyProject
Namespaces in PHP work like folders for your functions, classes, and constants.
They prevent name conflicts and keep code organized.
1. Defining a Namespace and Function
For example, if you add a function under Global Code:
<?php
namespace PHPMaker2026\MyProject;
function SayHello(string $name): void {
echo "Hello, $name!";
}
The full name of this function is:
PHPMaker2026\MyProject\SayHello
2. Calling a Namespaced Function
Option 1 - Use the full function name:
\PHPMaker2026\MyProject\SayHello("World");
Option 2 - Add a use function statement:
use function PHPMaker2026\MyProject\SayHello;
SayHello("World");
3. Calling Built-in Functions
In most cases, you can call built-in PHP functions (such as date(), strlen(), array_merge()) directly inside a namespace — PHP will automatically find the global version.
namespace PHPMaker2026\MyProject;
function ShowDate(): void {
echo date('Y-m-d'); // no backslash needed in normal cases
}
You only need to add a leading backslash if a local function with the
same name exists and you want to call the global one:
namespace PHPMaker2026\MyProject;
function strlen($s) { return 999; }
echo strlen("abc"); // calls local version
echo \strlen("abc"); // calls global PHP function
4. PHPMaker PSR-4 Autoloading
PHPMaker maps its namespace to multiple base folders:
"autoload": {
"psr-4": {
"PHPMaker2026\\MyProject\\": [
"src/",
"models/",
"controllers/"
]
}
}
That means PHP will find files under any of these directories:
Namespace → File Path
PHPMaker2026\MyProject\Helper → src/Helper.php
PHPMaker2026\MyProject\UserTable → models/UserTable.php
PHPMaker2026\MyProject\HomeController → controllers/HomeController.php
Most generated files share the same namespace, so you can call project functions and classes directly — they behave almost like global ones.
However, understanding namespaces is still important when using external libraries or sub-namespaces.