0

Can I dynamically name a class?

I have written a process which calls about 6 pages that generate pdfs through tcpdf. They all use the same header page which makes a class.

class myPDF extends TCPDF {

the problem is when i run my script I get Fatal error: Cannot redeclare class myPDF in after the first page is done.

I understand why but am at a loss how to deal with it.

calling it only once is no good as it holds a function which looks at page specific variables.

I also don't think I can UNdeclare it in anyway.

Once way would be to change the class name for each include but not sure how to make the class name a variable?

ie class $newname

Any ideas

S

Added this

class myPDF extends TCPDF {
public function Header () {

global $title;
global $client;
global $host;


$oMulticell->multiCell(266,8, "<s1>$title&nbsp;     Client:</s1> <s2>$client</s2>     <s1>Host:</s1> <s2>$host</s2>     <s1>Currency:</s1> <s2>$currency</s2>     <s1>Reporting Period:</s1> <s2>$start_date_rep to $end_date_rep</s2>", 'TB');
}

2 Answers 2

1

include_once(); should fix your problem.

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

4 Comments

each time i run an include it uses page specific variables inside the class - if I use it once - it doesnt look to the new variables
Your code should be fine if you make sure that wherever you include the myPDF class or TCPDF class, you change include to include_once. The reason it works with one include is because each subsequent include is trying to redefine myPDF, whereas if you instead do include_once then it won't have that issue! Try that and it should be fixed. :) Edit: do you really need to generate those page specific variables inside the class?
ok emma - i had to muck about with some includes and put the class in its own page but you were completely right. Do I need to generate within the class - not sure but i am following tcpdf examples which did so . No problem for a single pdf but one after the other driven from a master page caused my issue. thanks for the guidance
Ideally, you should just put a class in its own php file, with nothing but just that class in there -- this is one reason why you should do that. Also I've got a job, thanks though lol.
0

You can't. Class definition has to be parsable in compile-time, and not in run-time. You can't make a "dynamic" class name.

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.