Namespaces help organize code by grouping classes that logically belong together. Autoloading helps in automatically loading classes without manually including them.
// src/Controllers/ExampleController.php
namespace AppControllers;
class ExampleController {
public function sayHello() {
return "Hello, World!";
}
}
// src/Models/ExampleModel.php
namespace AppModels;
class ExampleModel {
public function getData() {
return "Data from model.";
}
}
<?php
require 'vendor/autoload.php';
use AppControllersExampleController;
use AppModelsExampleModel;
$controller = new ExampleController();
echo $controller->sayHello();
$model = new ExampleModel();
echo $model->getData();
?>
New contributor
Sarbjit Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1