pascoea Posted February 24, 2010 Share Posted February 24, 2010 Hey there, how's everybody doing today? I have to start by saying this is the first time I have ever posted a coding question to a forum. I don't want to be the guy who asks a forum before doing some searching, but I've been beating my head against this one for a while. I've done a little with Object Oriented Programming on other languages, but not well versed in OOP in PHP. I have 2 files that generate different reports, report1.php and report2.php. Which report is needed is dictated by a value stored in a DB. Inside both of the report files the "report" function returns the desired report output: <? report($id){ //html is generated and stored in $out reuturn($out); } ?> Ok, in the main program I am trying to generate a string of reports. Basically a batch print of the days time cards. function getReportBatch($date){ //sql stuff to get the $result variable //row "report" contains a string dictating which report to use //row "id" contains the id number to the individual time card while($row=mysql_fetch_array($result)){ if(!isset($reports[$row['report']])){ //make an object for the requested report if it isn't created yet $reports[$row['report']]=new reportHandler($mainDocRoot.'reports/'.$row['report']); } //stuff the html for the report into the output variable $out.=$reports[$row['report']]->getReport($row['id']); } return $out; } class reportHandler{ function __construct($report){ require($report); } function getReport($tcId){ print report($tcId); } } All goes well until the script runs into a different report. Lets say entries 1 through 5 use "report1.php" and entry 6 needs "report2.php", the script runs fine till it gets to the include while processing entry 6. I get the following error: Fatal error: Cannot redeclare report() (previously declared in ../reports/report1.php:2) in ../reports/report2.php on line 269 I understand the simple solution would be to create a unique function names inside of report1.php and report2.php, but I really don't want to do that as it would cause a significant amount of modification to several other modules. Any thoughts? Thank you so much! Link to comment https://forums.phpfreaks.com/topic/193264-help-with-an-include-inside-of-a-class/ Share on other sites More sharing options...
jl5501 Posted February 24, 2010 Share Posted February 24, 2010 What seems odd to me here is that you have several functions with the same name which presumably do different things for each report, and, as you have discovered, you cannot define a function more than once. Is there any reason why the function cannot be parameterised so that it can handle all the report options from its original definition Link to comment https://forums.phpfreaks.com/topic/193264-help-with-an-include-inside-of-a-class/#findComment-1017650 Share on other sites More sharing options...
pascoea Posted February 24, 2010 Author Share Posted February 24, 2010 The idea was to be able to keep the reports modular. When I need to generate a different report in the future I can just drop in another one without having to modify anything in the main program. Link to comment https://forums.phpfreaks.com/topic/193264-help-with-an-include-inside-of-a-class/#findComment-1017663 Share on other sites More sharing options...
jl5501 Posted February 24, 2010 Share Posted February 24, 2010 I can see what you are trying to achieve, but not sure having different versions of a function is the way to go about it. One way forward with your approach, would be to have report() as a fixed wrapper function, that included the code it needed to run from a different file, not the function definition itself. You could have different named functions then, that were called from report() depending on its parameter Link to comment https://forums.phpfreaks.com/topic/193264-help-with-an-include-inside-of-a-class/#findComment-1017677 Share on other sites More sharing options...
pascoea Posted February 24, 2010 Author Share Posted February 24, 2010 I was hoping to not have to do that... Oh well, I've had enough fun for the day. report1.php and report2.php: <? //html is generated and stored in $out ?> main prog: function getBatchReport($date){ while($row=mysql_fetch_array($result)){ if(!isset($reports[$row['report']])){ $reports[$row['report']]=new reportHandler($mainDocRoot.'reports/'.$row['report']); } $out.=$reports[$row['report']]->getReport($row['id'],-1,false); } } class reportHandler{ public $pathToFile; function __construct($report){ $this->pathToFile=$report; print $report.'-'.$this->pathToFile; } function getReport($tcId,$custId,$aprId){ include ($this->pathToFile); return $out; } } I call that the "hell with it, it works" theory. +) Thanks for your input JL. Link to comment https://forums.phpfreaks.com/topic/193264-help-with-an-include-inside-of-a-class/#findComment-1017688 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.