Lets say we have the following PHP classes
// Books/MyUtility.php
class MyUtility {
public static function export(): void {
echo "export function" . PHP_EOL;
}
}
// Authors/MyUtility.php
class MyUtility {
public static function exportAll(): void {
echo "exportAll function" . PHP_EOL;
}
}
Lets require them both in our start php file and see what happens
// start.php
require('Books/MyUtility.php');
require('Authors/MyUtility.php');
MyUtility::export(); // from Books/MyUtility.php
MyUtility::exportAll(); // from Authors/MyUtility.php
-> % php start.php
PHP Fatal error: Cannot declare class MyUtility, because
the name is already in use in
/Users/kevin/cakefest/Authors/MyUtility.php on line 3
PHP class names need to be unique?
PHP class names need to be unique?
NO!We need to add Namespaces to our classes
// Books/MyUtility.php
class MyUtility {
public static function export(): void {
echo "export function" . PHP_EOL;
}
}
// Books/MyUtility.php
namespace Books;
class MyUtility {
public static function export(): void {
echo "export function" . PHP_EOL;
}
}
// Authors/MyUtility.php
namespace Authors;
class MyUtility {
public static function exportAll(): void {
echo "exportAll function" . PHP_EOL;
}
}
-> % php start.php
PHP Fatal error: Uncaught Error: Class 'MyUtility'
not found in /Users/kevin/cakefest/start.php:6
Stack trace:
#0 {main}
thrown in /Users/kevin/cakefest/start.php on line 6
Well, another error 🙈
PHP doesn't know which Class to use
We have to tell it which to use
// start.php
require('Books/MyUtility.php');
require('Authors/MyUtility.php');
MyUtility::export(); // from Books/MyUtility.php
MyUtility::exportAll(); // from Authors/MyUtility.php
// start.php
require('Books/MyUtility.php');
require('Authors/MyUtility.php');
Books\MyUtility::export(); // from Books/MyUtility.php
Authors\MyUtility::exportAll(); // from Authors/MyUtility.php
-> % php start.php
export function
exportAll function
success 🙌
Lets just look into the src/Controller/AppController.php
namespace App\Controller;
use Cake\Controller\Controller;
class AppController extends Controller
{
....
}
We can see, that
do we start with an App namespace and not with a src namespace?
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
With this composer automatically loads all classes in the folder src in the namespace App
"autoload": {
"psr-4": {
"Cake\\": "src/"
}
},
https://github.com/cakephp/cakephp/blob/master/composer.json
"autoload": {
"psr-4": {
"DebugKit\\": "src/"
}
},
https://github.com/cakephp/debug_kit/blob/master/composer.json
Lets continue in the editor
// Missing one of these
use App\Utility\CoinSync;
$coinSync = new \App\Utility\CoinSync();
function sync() {
use App\Utility\CoinSync;
$coinSync = new CoinSync();
}
Parse error: syntax error, unexpected token "use" in
src/Controller/CoinsController.php on line 110
The 'use' keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations
namespace App\Utility requires a folder structure of src/Utility
Duplicate code used only in