PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 6, 2022

[FIXED] Composer: Fatal error: Uncaught Error: Call to undefined function

 January 06, 2022     composer-php, php     No comments   

Issue

I have my own functions stored in the helpers.php file in the helpers folder relative to the composer.json file.

<?php

function config (string $params){}

function redirect() {}

In the file composer.json this file is included in autoload

"autoload": {
    "psr-4": {
      "App\\" : "./app"
    },
    "files": [
      "helpers/helpers.php"
    ],
  "scripts": {
    "delete-all-tables": "App\\Migrations\\DeleteTable::deleteAllTables",
  }
}

I used composer-dump after connecting helpers.

I am using config () at this location:

<?php

namespace App\Migrations;

use App\Components\Migration;

class DeleteTable extends Migration
{
    public static function deleteAllTables()
    {
        $param = config('db.dbname');

        $instance = new self();
        $instance->con->query("DROP DATABASE " . $param . "; CREATE DATABASE " . $param . "; USE " . $param . ";");
    }
}

When using my functions in the Migration class not through the composer, everything works correctly, but when calling scripts commands through Terminal, the config function is not executed. Then the error appears:

Fatal error: Uncaught Error: Call to undefined function App\Migrations\config() in D:\OSPanel\domains\myshop\app\Migrations\DeleteTable.php:17
Stack trace:
#0 phar://C:/composer/composer.phar/src/Composer/EventDispatcher/EventDispatcher.php(324): App\Migrations\DeleteTable::deleteAllTables(Object(Composer\Script\Event))
#1 phar://C:/composer/composer.phar/src/Composer/EventDispatcher/EventDispatcher.php(218): Composer\EventDispatcher\EventDispatcher->executeEventPhpScript('App\\Migrations\\...', 'dele
teAllTables', Object(Composer\Script\Event))
#2 phar://C:/composer/composer.phar/src/Composer/EventDispatcher/EventDispatcher.php(101): Composer\EventDispatcher\EventDispatcher->doDispatch(Object(Composer\Script\Event))
#3 phar://C:/composer/composer.phar/src/Composer/Command/ScriptAliasCommand.php(64): Composer\EventDispatcher\EventDispatcher->dispatchScript('delete-all-tabl...', true, Array)
#4 phar://C:/composer/composer.phar/vendor/symfony/console/Command/Command.php(245): Composer\Command\ScriptAliasCommand->execute(Obje in D:\OSPanel\domains\myshop\app\Migrations\Delet
eTable.php on line 11

Can you please tell me what am I doing wrong?

Are there any options for solving the problem without using helpers in the class? Thank you very much for your help!


Solution

According to the Composer documentation:

Callbacks can only autoload classes from psr-0, psr-4 and classmap definitions. If a defined callback relies on functions defined outside of a class, the callback itself is responsible for loading the file containing these functions.

so any files normally included as part of a files autoloader type won't be included in this specific case.

As I see it, to get around this you have two options:

  • Move the helper functions into a helper class as static member functions and have that loaded via the Composer autoload mechanism (either psr-0 or psr-4)
  • Determine the location of the helpers.php file and require that in the migration file

For the first, it would look something like (in app/Helpers.php):

<?php

namespace App;

class Helper {
  static function config (string $params){}

  static function redirect() {}
}

which would be called thus:

\App\Helpers::config('db.dbname');

In the second, taking inspiration from 37925437, your DeleteTable.php could end up something like:

<?php

namespace App\Migrations;

use App\Components\Migration;

class DeleteTable extends Migration
{
    public static function deleteAllTables()
    {
        require(dirname(\Composer\Factory::getComposerFile()) . '/helpers/helpers.php');

        $param = config('db.dbname');

        $instance = new self();
        $instance->con->query("DROP DATABASE " . $param . "; CREATE DATABASE " . $param . "; USE " . $param . ";");
    }
}


Answered By - msbit
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing