1

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.

5
  • where's your code, mate ? Commented Mar 10, 2016 at 11:01
  • What exactly do you mean by "I get problems"? What entries do you get in your error log file? Commented Mar 10, 2016 at 11:02
  • 'PHP Fatal error: require_once(): Failed opening required <filepath>(include_path='.;C:\php\pear') in <filepath> on line 29' Commented Mar 10, 2016 at 11:04
  • The other problem is that some pages just hang without any error message. Commented Mar 10, 2016 at 11:05
  • There is a vast amount of code in this project. I have no idea which parts are relevant to this issue. What part of the 10,000 lines of code do you want me to post? Commented Mar 10, 2016 at 11:07

2 Answers 2

2

You should use "use" operator in your file by below way.

use Tcpdf\Extension\Table\Table as TableClass;

after this, "new TableClass();" would instantiate a Tcpdf\Extension\Table\Table Class

For more info, check below link:-

PHP namespaces and "use"

Hope it will help you :-)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.