How can plugins/modules be implemented in PHP without altering the original source code?

To implement plugins/modules in PHP without altering the original source code, you can use the concept of hooks and filters. Hooks allow you to "hook into" specific points in the code where you can execute custom functionality, while filters allow you to modify data before it is output. By creating a system of hooks and filters in your PHP application, you can easily extend its functionality without directly modifying the core code.

// Define a hook system
$hooks = [];

// Register a hook
function add_hook($hook_name, $callback) {
    global $hooks;
    $hooks[$hook_name][] = $callback;
}

// Execute a hook
function do_hook($hook_name, $args = []) {
    global $hooks;
    if (isset($hooks[$hook_name])) {
        foreach ($hooks[$hook_name] as $callback) {
            call_user_func_array($callback, $args);
        }
    }
}

// Register a hook
add_hook('my_custom_hook', function() {
    echo 'Custom functionality added!';
});

// Execute the hook
do_hook('my_custom_hook');