SarahK
SarahK

Reputation: 59

PHP: Class not found tcpdf wrong path

I got some SVG-Charts and now I want to be able to download them as a PDF-File from my website. To do so I want to use TCPDF. I have required the file of the library and give out the used path, also I used "class_exists('TCPDF')". Both return the right value. Still, I get the following error:

Fatal error: Uncaught Error: Class 'Kanboard\Controller\TCPDF' not found in C:\xampp\XAMPP\htdocs\fftboard\app\Controller\ExportController.php:172 Stack trace: #0 C:\xampp\XAMPP\htdocs\fftboard\app\Controller\ExportController.php(117): Kanboard\Controller\ExportController->downoadPDF(Array) #1 C:\xampp\XAMPP\htdocs\fftboard\app\Core\Controller\Runner.php(77): Kanboard\Controller\ExportController->summaryPDF() #2 C:\xampp\XAMPP\htdocs\fftboard\app\Core\Controller\Runner.php(31): Kanboard\Core\Controller\Runner->executeController() #3 C:\xampp\XAMPP\htdocs\fftboard\index.php(13): Kanboard\Core\Controller\Runner->execute() #4 {main} thrown in C:\xampp\XAMPP\htdocs\fftboard\app\Controller\ExportController.php on line 172

This is strange, since the class is not 'Kanboard\Controller\TCPDF' but 'Kanboard\Analytic\TCPDF'. Has this something to do with the namespace? I do not know, why it can not find the class, if the file is required correctly. Some code of the file:

namespace Kanboard\Controller;
class ExportController extends BaseController
{
  public function downoadPDF($project)
  {
    $tcpdfPath = realpath( dirname( __FILE__ ).'/../../libs/tcPDF/TCPDF-master/TCPDF-master/tcpdf.php');
    require_once $tcpdfPath;
    $log .=  class_exists('TCPDF');
    $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
  }
}

In "PHP class not found but it's included" the Problem is not solved. The answer marked as solutin just tells you to check if the File exists and it does. The other solutions suggests to use <?php instead of <?, which I already did bevore and to check if the Class exists, which it did, as I said above.

Can somebody explain to me why this error appears?

Upvotes: 1

Views: 3356

Answers (1)

bishop
bishop

Reputation: 39394

When you declare:

namespace Kanboard\Controller;

all partially-qualified class names will be taken relative to that name. Your code here:

new TCPDF(...)

uses a partially-qualified class name -- since it doesn't start with a \ -- so, it's resolved to Kanboard\Controller\TCPDF.

You've stated it should be Kanboard\Analysis\TCPDF, but I'm not so sure based on how the code is written. Try updating your code to either of these:

new \TCPDF(...)
new \Kanboard\Analysis\TCPDF(...)

Replace the ... with the actual arguments. If the first works, then TCPDF is in the root namespace (which I suspect). If the second works, then it's in the namespace you stipulate.

Upvotes: 1

Related Questions