I'm getting this error when I try to call a function before referencing another class the exists in the same file as the function I'm trying to call.
This error don't occur if the line // new ICMS;
is uncommented.
Composer json
"autoload": {
"psr-4": {
"Gbbs\\NfeCalculos\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Gbbs\\NfeCalculos\\Tests\\": "tests"
}
},
Folder structure
nfe-calculos/
├── src/
│ └── ICMS.php
└── tests/
└── ICMSTest.php
ICMS.php
<?php
declare(strict_types=1);
namespace Gbbs\NfeCalculos;
use Exception;
class ICMS
{}
function pICMSFromUFs(string $ufOrigem, string $ufDestino): float
{
throw new Exception('UF inexistente: ' . $ufOrigem . ' - ' . $ufDestino);
}
ICMSTest.php
<?php
declare(strict_types=1);
namespace Gbbs\NfeCalculos\Tests;
use Gbbs\NfeCalculos\ICMS;
use PHPUnit\Framework\TestCase;
use function Gbbs\NfeCalculos\pICMSFromUFs;
class ICMSTest extends TestCase
{
/**
* Test invalid UFs
*/
public function testInvalidUFsInpICMSFromUFs()
{
// new ICMS;
$this->expectException('\Exception');
pICMSFromUFs('1', '1');
}
}
CodePudding user response:
If you just want to call the function, then you will have to include the source file (require_once()
or similar).
What is happening is that the autoloader is detecting you want to use the ICMS
class and so it's loading the file for you. This includes the function you are calling.
You could do something like add an echo
in the ICMS.php source file and you should see this being displayed to prove this.
TBH - a source file should only really be the class, if the function is something to do with the class, you possibly could add it as a static
method and call it as
ICMS::pICMSFromUFs('1', '1');