John Davidson

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

0 comments
Message:


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



  • composer.json

  • App

    • Utils

      • helpers.php





  • public

    • index.php




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



My Answer:
{
"autoload": {
"files": ["App/Utils/helpers.php"]
}
}

Rate this post

3 of 5 based on 7514 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us