php how to redeclare class in a for loop - php

Im using a library called PDFmerger , naturally, it merges pdfs together. I wish to run a cron every night that builds these to have them ready for the next day. The issue is when I run the code after the first one it fails because it is attempting to redeclare the class.
The for statement...
foreach ($pdfs_to_build as $pdf) {
// do stuff
$this->merge_pdfs($pdf['pdf_id']);
}
And my pdf merging code that gets ran in the loop...
$pdf = new PDFMerger;
$pdf->addPDF(DIR_BOOKS.$book_path.'/static-pages/page-1.pdf', '1');
$pdf->addPDF(DIR_FINAL_PDFS.$pdf_id.'/custom-page-1.pdf','1');
$pdf->addPDF(DIR_BOOKS.$book_path.'/static-pages/page-2.pdf', '1');
$pdf->merge('file', DIR_FINAL_PDFS.$pdf_id.'/final-build-'.$pdf_id.'.pdf');
Error after the first 1 is complete and we've onto the next one...
Fatal error: Cannot redeclare class PDFMerger in /var/www/example/pdfmerger/PDFMerger.php on line 24
Im wondering is there a workaround for this?

The problem isn't that you're calling new PDFMerger twice, that's perfectly acceptable. The problem is that the file:
/var/www/example/pdfmerger/PDFMerger.php
Is being include'd (or otherwise executed) twice, which has on line 24:
class PDFMerger {
That is what is triggering your error, that class has already been declared. Figure out why that file is being included twice and your error will clear up.

Your code is fine. That won't fail. However, not listed in what you posted, you are including PDFMerger.php.
Probably by having the include 'PDFMerger.php'; shown at https://pdfmerger.codeplex.com/ run inside the loop.
Just move that line out of the loop and/or change the include to include_once.

Related

Why im getting function redeclared fatal error when i have declared it only once?

function createStudentMarks($key)
{ echo "key".$key;exit; }
This is my function.
$StudentMarks=createStudentMarks($key);
And i have called function in same file like this n im getting fatal error. Y is it so can anyone please help me
Cannot redeclare createStudentMarks() (previously declared in /var/www/html/2016/S/Mcd_BuildStudentsMarks.php:85) in /var/www/html/2016/S/Mcd_BuildStudentsMarks.php on line 85 This is the error wat im getting exactly
Most of the times, this is caused by accidentally requireing or includeing a file more than once.
This generally shows bad coding behavior, so first try to find if this is the case or not. Try to eliminate the second and so on requires or includes.
In any case, you can work around it but using include_once or require_once, which will not include the file on second or next calls.

Include PHP File and execute it multiple times

Yeah I know this is probably not the best way to perform this. However I have a script which I would like to call multiple times with parameters and hand over two variables. The code I have so far looks like this:
$testfeld = array('User1'=>'400028',
'User2'=>'400027'
);
foreach($testfeld as $key=>$value) {
$_GET['cmd'] = 'modify';
include("testmodify.php");
}
If I run this code, I get an error message:
Fatal error: Cannot redeclare show() (previously declared in testmodify.php:14) in testmodify.php on line 39
If I perform this, it only modifys me the first entry of the array and then throws me the error message above.
Sadly, I have only read access to testmodify.php.... So is there a way to perform this include with all the content of the array?
Put your code in the function, include it once and call as much as you want.
You are receiving this error because the file testmodify.php contains the definition for "show". A second inclusion of the file caused by your loop attempts to define the object show again, which cannot be done.
Here is another SO question about someone trying to redefine a function.
Php and redifining
Edit: the other answer by MilanG is how you could go about fixing it.
You can do something like (in your testmodify.php script):
if (!function_exists("show")) {
function show($parameters) {
/* do your stuff here */
}
}
A better thing to do, in this case, would be to perform include_once() in your testmodify.php script, making it point to a script file where you define your show() function. My approach should also work for you.
Hope that helps :)

ModX Evo: PHP error in document.parser.class.inc.php

So my Evo site stopped working the other day - just got a 500 error. I got my host to check the logs and found this:
[error] PHP Fatal error: Cannot redeclare insert_metka() (previously declared in
/home/mysite/public_html/manager/includes/document.parser.class.inc.php(790) : eval()'d code:2)
in /home/mysite/public_html/manager/includes/document.parser.class.inc.php(790) : eval()'d code on line 12
I have tired commenting out the offending line and removing the entire file to no avail. Does anyone know what this means and how to fix it?
EDIT: the code at line 790:
eval ($pluginCode);
Looks like a bad plugin has broken your site. Disable all your plugins and reinstate them one at a time until it breaks again, then you know which one is the culprit.
Once you've done that, post the plugin code here and we can help you debug it further. You shouldn't ever need to modify the MODX source code.
The problem is likely to be solved by wrapping the insert_metka() declaration like this:
if(!function_exists('insert_metka')) {
function insert_metka() {
// function code
}
}
http://wiki.modxcms.com/index.php/Creating_Snippets#Wrap_functions_inside_.21function_exists_conditional_if_the_resource_is_needed_to_run_more_than_once_on_a_page
That appears like a very simple problem. You are declaring insert_metka() two times. Remove it from one of the mentioned files. Once it is declared in saved file and apparently second time you are trying to declare it with the help of eval()
Cannot redeclare insert_metka() says that you are declaring a function twice (may be with your evals).
You should check on your included files.
To be sure to not include more than one time, you can use include_once or requiere_once instead of include and requiere
Accoring to document.parser.class.inc.php this line i guess you are using OOP.
So what you can do is create instantiation class for above class and then overwrite it.
There are other things too. you might declaring same function inside of same class.

Instantiating a new PHPCrawl Class throws the error "Call to undefined method stdClass::receivePage()"

I use a foreach loop to loop through multiple seed urls. During each loop, I instantiate a crawler using PHPCrawl and the next seed url.
foreach($companyUrls as $companyId => $companyUrl) {
$crawler = new MyCrawler($companyUrl, $companyId);
$crawler->go();
}
It runs fine for the first loop, but throws the following error the second time through before any crawling has been done:
"Call to undefined method stdClass::receivePage() in
/data/utilities/PHPCrawl_070/classes/phpcrawler.class.php on line 201"
I have not modified the original PHPCrawl V0.70 classes in any way. I have only extended the PHPCrawler class and added process code to the handlePageData() function as described in the PHPCrawl documentation. If I run this without the foreach loop (instantiating a new class one at a time with new urls), the system works fine.
Please help!!
This is a known error and has been fixed in Version 0.71 (I was using version 0.70).
Thanks #prodigitalson for your comment.

php fatal error- no idea why

im getting this error message
Fatal error: Cannot redeclare get_db_conn() (previously declared in `/home/maxer/domains/x/public_html/xmasapp/dbfuncs.php:21) in /home/maxer/domains/x/public_html/xmasapp/dbfuncs.php on line 24`
this is the code
function get_db_conn() {
$conn = mysql_connect($GLOBALS['db_ip'], $GLOBALS['db_user'], $GLOBALS['db_pass']);
mysql_select_db($GLOBALS['db_name'], $conn);
return $conn;
}
line 21 refers to
$conn = mysql_connect($GLOBALS['db_ip'], $GLOBALS['db_user'], $GLOBALS['db_pass']);
line 24 is the closing curly bracket of the function
the code worked fine until I tried to clean my code up, I ripped most of the "view" code out and put it into separate files but didn't change any logic
You are most likely including a file twice or including two files that include the same file each.
You can prevent this by using include_once() or setting up a better structure of what you include when.
EDIT
Try this and see if you see an error in your include setup.
echo "<pre>";
print_r(get_included_files());
echo "</pre>";
Somewhere you're including a file twice or some two files has a definition of your function.
Is this in an includes file? Is the includes file getting included more than once?
It's complaining because the get_db_conn is defined more than once, and most likely it's getting included multiple times unless you have that function in two different places.
Your error message says:
Cannot redeclare get_db_conn() (previously declared in [...]/dbfuncs.php:21) in [...]/dbfuncs.php on line 24
You have a function named get_db_conn() that you are declaring multiple times. Is your dbfuncs.php file including itself?
It's possible you're include()ing the same file more than once. For this reason I like to use include_once() whenever possible.
You declare two functions with identical names (both are called get_db_conn())

Categories