Search code examples
phpnamespacestcpdf

PHP Class using namespaces causes problems with my code that doesn't use namespaces. Why?


I'm trying to use a php library called tcpdf-extension. This library uses namespaces and 'use' commmands. The rest of my code does not. I'm pretty new to namespaces and do not understand how to use them.

If I include the library, I get problems with php not being able to find other included/required files. For instance: 'PHP Fatal error: require_once(): Failed opening required <filepath>(include_path='.;C:\php\pear') in <filepath> on line 29' Another issue is some of my previously working website pages just hang without any error message. If I remove the include for the library, everything goes back to normal.

If I use the library in a separate page from the rest of my code, then there's no problems, but as soon as I include it in any other page, it fails.

Why is this happening and what can I do to fix it?

Update: example of including the library:

require_once ('tcpdf/Extension/Helper.php');
require_once ('tcpdf/Extension/Table/Table.php');
require_once ('tcpdf/Extension/Table/Cell.php');
require_once ('tcpdf/Extension/Table/Row.php');
require_once ('tcpdf/Extension/Table/TableConverter.php');
require_once ('tcpdf\Extension\Attribute\AbstractAttribute.php');
require_once ('tcpdf\Extension\Attribute\BackgroundAttribute.php');
require_once ('tcpdf\Extension\Attribute\BackgroundFormatterOptions.php');

if this is commented out, the problems go away, but I need to use this library.

Here's a section from Table.php:

<?php
namespace Tcpdf\Extension\Table;
class Table{
...

Here's a section from Cell.php:

namespace Tcpdf\Extension\Table;

use Tcpdf\Extension\Attribute\BackgroundAttribute;

class Cell
{
...

My guess is that the use and/or namespace commands have something to do with it because that's the only thing about this library that is different than other libraries I've used without any problems. I've also tried commenting out the use commands and that makes code outside the library work okay, but it makes the library not work. Perhaps after I include this library I need to give another 'use' command to get back to the right namespace for the rest of my code. However, since I've never set a namespace for the code, I don't know what the use command would be.

This is the library in question: https://github.com/naitsirch/tcpdf-extension

Another detail that might be relevant: Most of my code is procedural style. I often make use of classes, but most of this (very large) code base is not in a class at all. If it were all in classes, I'm sure I could add use statements for each class, but that is not the case.


Solution

  • The only way I have found to use this library with my existing, non-namespaced, procedural code is to remove all use statements and namespace statements from the library. This library only has 8 files, so this wasn't hard. I'd still like to hear answers from people if they have a better approach, as I'm sure this issue will come up again.

    Update: Installing the library with Composer also fixes this issue.