Leon Kardaš
Leon Kardaš

Reputation: 101

Composer: Autoloaded file helper.php is autoloaded but the functions inside it are not

I am trying to make my own mock MVC framework as a project. This is my first time using composer outside of using it for requiring dependencies for Laravel. The actual autoloading works well, but when I try to autoload the helpers.php something weird happens. The file is autoloaded(if I change the path of the file I get the file not found error) but the contents inside it are not. In another file I try to call any function from the helpers.php file and I get

Fatal error: Uncaught Error: Call to undefined function

This is the file structure of the example

This is my composer.json file:

{
    "name": "admin/projecttest",
    "autoload": {
        "psr-4": {
            "Admin\\Projecttest\\": "src/",
            "App\\": "App/"
        },
        "files": [
            "App/Utils/helpers.php"
        ]
    },
    "minimum-stability": "dev"
}

The helpers.php

<?php

namespace App\Utils;

use Leonlav77\Frejmcore\helpers\DotEnv;

function config($config){
   $config = explode(".", $config);
   $file = $config[0];
   $configFile = require "../config/$file.php";
   return $configFile[$config[1]];
}

function env($key, $default = null){
   (new DotEnv(__DIR__ . '../../.env'))->load();
   return getenv($key) ? getenv($key) : $default;
}

function baseDir(){
   return __DIR__ . "/../";
}

index.php (where I call the function from the helper)

<?php

require "../vendor/autoload.php";


var_dump(function_exists('baseDir'));
var_dump(baseDir());

from the function_exists I get false

Upvotes: 1

Views: 265

Answers (1)

Leon Kardaš
Leon Kardaš

Reputation: 101

As the user Foobar suggested the the problem was in the namespace of the helpers.php . Since it had a namespace the functions also had a namespace, so insted of baseDir() I needed to use App/Utils/baseDir(). The solution was to simply remove the namespace from helpers.php

Upvotes: 1

Related Questions