PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon May 22, 2017 4:23 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Tue Sep 01, 2015 7:29 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
I have the following pages:

1. Class page
2. 'Controller' page
3. index page

On the controller page, I have the following code

Syntax: [ Download ] [ Hide ]
<?php
  // included class page, created object
try {          
   $data = $prodObj->getRandomProducts();
   if ($data !== FALSE) {
     $productArray = $data;
   }
   else {
      // no data in array, 0 records.
   }           
}
   catch (Exception $e) {
     var_dump($e->getTrace());
   }
?>
 


On the index page I want to use $productArray to display products.

Syntax: [ Download ] [ Hide ]
  // index page, display data
  <p><?php echo $productArray['product_id']; ?></p>
 


Where do I catch the exception, on the controller page or on the index page? Could I catch the exception inside the class, in the wrapper function?

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Tue Sep 01, 2015 3:02 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6417
Location: WA, USA
Depends how you want to react to the exception.

If you want to show an error page, that suggests changing the view and it should be done in the controller.
If you want to show the same page but with an error message, that suggests doing it in the view. However I think I'd use a second view for it instead of passing the information along to your index view.

Not sure what the "wrapper function" is.


Top
 Profile  
 
PostPosted: Tue Sep 01, 2015 10:56 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
It's the public function inside the class that calls a protected function

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Sun Sep 06, 2015 4:29 am 
Offline
Forum Newbie

Joined: Mon Feb 15, 2010 7:01 am
Posts: 16
From the 3 aspects that you described ( 1. Class page 2. 'Controller' page 3. index page) I can't understand what is the “top” . Generally , exceptions should be cached through the top level component of an application. By top I mean the one that is in the core of the app and generates all the others (e.g. the index.php isn't the top normally because it is just the instantiator of the front controller in MVC). That doesn't mean that each tier of the app should try – catch , the opposite. They should if they have to do something with that exception and then through it back to parent. If children's or other tiers only catch the exception to through it up (or again) there is no meaning to do so and is a sign of a week understanding of what Exceptions are (in my opinion).


Top
 Profile  
 
PostPosted: Sun Sep 06, 2015 11:49 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13396
Location: New York, NY, US
It seems like $prodObj->getRandomProducts() should generate the exception and then whatever code above it can deal with the exception. Having that if() indicates that maybe you are calling things in the wrong place.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sun Sep 06, 2015 12:16 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
jkon wrote:
Generally , exceptions should be cached through the top level component of an application.

The top of these three would be the class; is your suggestion to catch the exception within the class?

Christopher wrote:
that maybe you are calling things in the wrong place.

It would be better to catch the exception within getRandomProducts() ?

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Sun Sep 06, 2015 3:48 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13396
Location: New York, NY, US
The question I would ask is: what is the exception communicating? Is it that some actual error occurred? Or is it just that no product records were found? For me the difference is what the View code displays to the user. If it is an error then I want to display something that the user can tell tech support that communicates the problem. If it is just no records then just display that no records were found.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Mon Sep 07, 2015 3:10 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
Christopher wrote:
The question I would ask is: what is the exception communicating? Is it that some actual error occurred?

The exception is to display an error, a database error in this case.

In the latest version of the code I've done the following:

- Moved the try / catch block into the class
- the 'controller' page creates the data
- index page uses the data

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Mon Sep 07, 2015 5:33 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13396
Location: New York, NY, US
It seem like the index page that uses the data should get the $prodObj object. It can then catch the exception if there is a database error, or separately check if no results are returned and let the user know about that.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Tue Sep 08, 2015 12:16 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
Christopher wrote:
It seem like the index page that uses the data should get the $prodObj object.

something to this effect:
Syntax: [ Download ] [ Hide ]
<?php
 // index page
 include_once 'controller.php';

 try {
  $data = $prodObj->getRandomProducts();
 }
 catch (Exception $e) {
   // display custom error
 }
?>
 

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Tue Sep 08, 2015 10:00 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13396
Location: New York, NY, US
Yes, I guess. It is still not clear which file is which.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Sep 09, 2015 12:05 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
this would be on the index page

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Wed Sep 09, 2015 9:47 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13396
Location: New York, NY, US
I guess my question is: Why do you need an exception? It seems like if($data) is all you need. Either some random products are found, or the array is empty. If there is a syntax error it will show up in the logs. An SQL error should be fixed in development. So either display the random products or show that there are none.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Sep 09, 2015 3:13 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
Christopher wrote:
Why do you need an exception?

currently it helps with debugging but should something happen during the query process i'd like the application to degrade as gracefully as possible - replace the error message with something more user friendly and email the error message to an administrator

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group