My requirement might seem like awful practice, but none the less I would like to know if it's doable.
I'm using composer to autoload my classes. In one of the class files, I want to define a function that can be used as a shorthand within the global namespace. Because of composer's requirements, the class needs to be namespaced.
My question: Is there any way via minor alterations that I could get this working?
MyClass.php:
<?php
namespace Jodes;
class MyClass {
public function __construct() {
echo "I am the class";
}
}
function fn(){
echo "I am a shorthand for doing stuff";
}
index.php:
<?php
require_once '../vendor/autoload.php';
use Jodes\MyClass;
new MyClass();
// Jodes\fn(); // works
// fn(); // doesn't work
composer.json
{
"name": "jodes/mypackage",
"autoload": {
"psr-4" : {
"Jodes\\" : "src"
}
}
}
I've tried everything I can think of without luck, despite reading more links than I can count.
Thanks