Bladescope Posted March 30, 2008 Share Posted March 30, 2008 Error: Fatal error: Call to a member function report() on a non-object in C:\AppServ\www\Portfolio\AppManv2\modules\questions.php on line 48 Line 48: $error->report("Step $step is non existant."); error class: <?php class error { // Set the Vars private $return = "<div style='width: 300px; border: 1px solid black'><h3>Error Report</h3>{report}</div>"; private $errortemp = "<p><b>Error:</b> {error}</p>"; private $errorcol; // Handle the report input public function report($report) { $this->errorAdd($report); } // Add an error to the report private function errorAdd($report) { $report = str_replace('{error}', $report, $this->errortemp); $this->errorcol .= $report; } // Generate Report public function getReport() { if ($this->errorcol) { $report = str_replace('{report}', $this->errorcol, $this->return); print $report; } } } $error = new error(); ?> The script is run from a file, which includes error.php at the top, then declares it's classes. A class called from lower down in the script includes questions.php, and generates that. Any tips, or do you need more info? Link to comment https://forums.phpfreaks.com/topic/98709-call-to-a-member-function-on-a-non-object/ Share on other sites More sharing options...
Bladescope Posted March 30, 2008 Author Share Posted March 30, 2008 Edit: I tested it on the same page and it worked, so this is how the pages are linked main page include()s error.php at the top of the script. main page then creates an abstract class which contains a public static method that includes questions.php. main page then calls (statically) the method. So, in theory, the following should happen Main page includes error.php -> error class is created -> error object is created Main page creates class + method containing include to questions.php Main page calls the method -> questions.php is included ->-> script runs a method from the error object except the script doesnt run a method because it says the method and object don't apparantly exist Link to comment https://forums.phpfreaks.com/topic/98709-call-to-a-member-function-on-a-non-object/#findComment-505159 Share on other sites More sharing options...
rofl90 Posted March 30, 2008 Share Posted March 30, 2008 try using if ( ! isset ( $error ) ) { $error = new error; } at the bottom of the file being included eg after the class definition, to prevent re-declarations Link to comment https://forums.phpfreaks.com/topic/98709-call-to-a-member-function-on-a-non-object/#findComment-505163 Share on other sites More sharing options...
Bladescope Posted March 30, 2008 Author Share Posted March 30, 2008 This apparantly works, so thanks, but this raises a little issue. You see, as you know from the error class script, it calls upon getReport to generate the error report. The getReport method is invoked after the method that includes questions.php has been invoked, yet it produces nothing. But if I place it in questions.php, it generates the report. Now, I could easily surpass it just by deleting the method and simply writing it to a file, adding it to a database or just by simply echoing it out each time an error occurs, but is there any way to keep the script and somehow get the class to work through include()? Link to comment https://forums.phpfreaks.com/topic/98709-call-to-a-member-function-on-a-non-object/#findComment-505168 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.