PHP Call to undefined function: Why Namespace Matters
You are encountering a Fatal error: Call to undefined function getInitialInformation() because you are trying to call a global function that does not exist in the current global namespace. This is a classic symptom of how PHP handles namespaces and scope resolution, specifically when mixing legacy-style functions with modern directory structures.
In your controller.php, you have defined an intake function. Inside it, you attempt to call getInitialInformation(). However, that function is defined in model/model.php. Even though you have included the file using require_once, PHP does not automatically place functions from that file into the global namespace of your controller.
The root cause lies in how the function was declared versus where it resides. If you look at the code in model/model.php:
function getInitialInformation($id) {
return $GLOBALS['em']->find('InitialInformation', $id);
}
This function is declared without a namespace prefix. In older PHP versions or loose configurations, functions defined in included files often leak into the global namespace of the script that includes them. However, modern PHP projects, especially those using frameworks or organized modularly, often rely on namespaces to prevent collisions and organize code logically.
If your model/model.php file is part of a namespace (for example, if it was intended to be namespace App\Model;), but you are calling it as a global function in controller.php, PHP looks for getInitialInformation in the current namespace of the controller, then the parent namespaces, and finally the global namespace. If the function isn't explicitly declared in one of those scopes, or if the file structure implies a namespaced context that isn't being bridged correctly, you get the "undefined function" error.
The most robust solution is to ensure both files share the same namespace declaration or to qualify the function call with the namespace prefix.
Option 1: Explicit Namespacing
If you want to maintain a clear separation of concerns where models live in a specific namespace, update your model/model.php to declare that namespace at the top:
<?php
namespace App\Models; // Example namespace declaration
class InitialInformationRepository {
public function getInitialInformation($id) {
// It is generally better practice to use classes for this logic
return $GLOBALS['em']->find('InitialInformation', $id);
}
}
Then, in your controller.php, you must import the class or prefix the function call if it remains a standalone function. If you keep getInitialInformation as a global function but wrap it in a namespace, you must ensure the caller knows about that scope:
// controller.php
require_once("model/model.php");
function intake() {
// Assuming getInitialInformation is in the same namespace or global
$info = getInitialInformation($id);
}
However, if model/model.php is actually inside a namespace (e.g., App\Model) and you are calling it globally, you face a scope issue. You must either:
- Remove the namespace from
model/model.phpentirely if you intend to use global functions for simple logic. - Call the function with the namespace prefix in
controller.php. Ifmodel/model.phpis in theApp\Modelnamespace, change line 24 incontroller.phpto:$info = App\Model\getInitialInformation($id);
Option 2: Refactoring to Classes (Recommended)
While fixing the scope issue works, relying on global functions for business logic is an anti-pattern. It makes testing difficult and leads to namespace pollution. The industry standard in PHP, particularly when using Entity Managers like Doctrine (indicated by $GLOBALS['em']), is to wrap this logic inside a class.
Refactor model/model.php into a dedicated service or repository class:
// model/InitialInformationService.php
namespace App\Services;
use Doctrine\ORM\EntityManager; // Or however your $em is injected
class InitialInformationService {
public function getInitialInformation($id) {
// Ensure $em is properly initialized, not via global if possible
return $this->entityManager->find('InitialInformation', $id);
}
}
Then, in controller.php, instantiate the service:
require_once("model/InitialInformationService.php");
function intake() {
$service = new App\Services\InitialInformationService();
// You might need to pass the EntityManager into the constructor
$info = $service->getInitialInformation($id);
}
Verification Steps
Before assuming a namespace is the issue, verify the physical state of your files:
- Check for
namespacedeclarations: Openmodel/model.php. If you seenamespace App\Model;, that is the culprit if you are calling it globally. If you don't see one, but the file path suggests a modular structure, check if another file included beforecontroller.phpsets a default namespace. - Check for Typo: Ensure the function name matches exactly, including case sensitivity. PHP functions are case-sensitive.
- Autoloading: If you are using Composer or an autoloader (like Laravel), do not use
require_oncemanually. The error suggests a manual include which might be bypassing the autoloader's namespace mapping.
If you are migrating from a legacy monolith to a structured application, this error is often the first indicator that your project structure needs to align with modern PHP standards regarding namespaces and class-based architecture.