?
Solved

try catch statement, need new set of eyes

Posted on 2009-02-23
3
Medium Priority
?
1,017 Views
Last Modified: 2012-05-06
Ok, I'm writing this project management type system out of boredom. It's framework is based off the Joomla 1.x framework even though I'm writing all of this from scratch. Anyway, I'll exclude all my code and just post a code example of what I'm trying to do here. Everything works except the try/catch part (the try block will execute fine, but if it encounters an error, it doesn't revert to the catch block).

What it's doing is reading a "cleansed" request variable called "component". It uses that to include the proper .php file from my /components/ directory, and create an instance of the class within this .php file and assign it to a property of my parent class... so that $this->component is now a pointer to the class inside of the included php file.

Now in my page template, in the content area, I simply call a $toDo->showContent() which is where I have a problem.. This function should first ATTEMPT to execute the components processRequest method (this is the included file remember).. however (this is purely for error reporting) if that method does NOT exist, OR the component wasn't included properly (for whatever reason), I'd like it to echo a generic error like "component does not follow coding guidelines"..


What happens is if the processRequest method exists, it is executed properly, however if I rename that method (to purposely make it try and execute a method that does not exist), it just shows a blank area where that content SHOULD have been (the rest of the page loads fine) and I get the PHP generated error "Call to undefined method com_pages::processRequests()".

Is this just improper use/understanding of how the try/catch system works or am I using it wrong or what? Hell even if I could do a

if typeof $this->component->processRequest is a method then execute else echo generic error. I just want a "fallback" if the method doesn't exist instead of a blank page that tells me nothing or a PHP generated error.
class toDo {
	public 		$component	= stdClass;
	
	public function processRequest($request) {
		$this->request = $this->cleanupRequest($request);
		unset($request);
 
		$component = "com_" . $this->request["component"];
		$componentFile = $this->path . "/components/" . $component . ".php";
 
		switch($this->request["component"]) {
			default:
				if (file_exists($componentFile)) {
					include_once($componentFile);
					$this->component = new $component();
				}
				else {
 
				}
				break;
		}
	}
	
	public function showContent() {
		try {
			$this->component->processRequest($this->request);
		}
		catch (Exception $e) {
			echo "component does not follow coding guidelines";
		}
	}
 
	private function cleanupRequest($data) {
		$data["component"] = preg_replace("/[^a-zA-Z0-9_]/", "", $data["component"]);
		$data["component"] = trim($data["component"]);
 
		$data["component"] = ($data["component"] == "") ? "pages" : $data["component"];
		return $data;
	}
}

Open in new window

0
Comment
Question by:MMDeveloper
  • 2
3 Comments
 
LVL 15

Author Comment

by:MMDeveloper
ID: 23712950
oh and sorry... $this->request is indeed declared as a public variable and contains the $_REQUEST data.
0
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 2000 total points
ID: 23713671
The try/catch mechanism will not catch fatal errors such as the "call to an undefined method" however in this particular case you can use the function_exists http://us3.php.net/function_exists function to check if the function is indeed present, if not you could raise a custom Exception http://us.php.net/exceptions which would be caught by the "catch" statement.

Alternatively you could also try to use the set_error_handler http://us2.php.net/set-error-handler function and define a custom error handler for errors.
0
 
LVL 15

Author Closing Comment

by:MMDeveloper
ID: 31550167
I knew I needed a new set of eyes I've stared at this too long. I could use that or a usage or get_class_methods, either way, thank you for showing me the light.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question