PHP Early Binding - Is there an alternative to requiring the file?

40 Views Asked by At

Premble: I am using composer and autoloader with PHP 8.

I have this class:

<?php

namespace Something\Cool;

class Test
{

}

interface TestStorage
{

}

class TestStoragePDOMySQL implements TestStorage
{

}

I need to use it as follows:

$storage = new TestStoragePDOMySQL();
$system = new Test();

This gives me the following error:

PHP Fatal error: Uncaught Error: Class "Something\Cool\TestStoragePDOMySQL" not found

After spending hours doing research the problem seems to be in the way PHP handles early binding as described here and not in the class that can't be found.

I managed to solve the issue in a way I don't like which is requiring the file instead of relying on autoloader:

require 'src/Something/Cool.php';
$storage = new TestStoragePDOMySQL();
$bucket = new Test();

There must be another way to use autoload instead of this solution but I can't find one. Any help?

0

There are 0 best solutions below