TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-26-2009, 01:21 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 170
Thanks: 8
captainmerton is on a distinguished road
Default How to cater for missing input to a class

Looking for some guidance on this. I'm taking in variables from a url using the $_GET function and passing these into a class. I'm doing all the validation in the class catering for every possible scenario however not sure how to handle situations where the $_GET variables are missing rom the url. Ideally i'd like to find a way to throw an exception from the class constructor if the correct data isnt passed in to the class however not sure what standard practice would be for this. Obviously a missing GET variable is causing a php system error as the class is missing input and I'd rather not encase class instantiation in an if statement in the client code. What is standard practice for making classes ropbust enough to handle missing input?
captainmerton is offline  
Reply With Quote
Old 08-26-2009, 03:37 PM   #2 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

I think you need to explain yourself a little better with examples too.

This does not make sense..."Obviously a missing GET variable is causing a php system error as the class is missing input and I'd rather not encase class instantiation in an if statement in the client code" - client code? PHP is server-side not client-side! Do you get an error or a notice?
__________________
JaoudeStudios.com | JaoudeStudios.com Forum | JaoudeStudios.com Blog
OpenSource is the road ahead...!
JaoudeStudios is offline  
Reply With Quote
Old 08-26-2009, 03:44 PM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 170
Thanks: 8
captainmerton is on a distinguished road
Default

Sorry by client code i mean the php code calling the class. Will try and be more specific:

If I create a class which requires 2 variables to be passed into it in order for it to be instantiated and I only pass in 1 then php will error. Is there a way i can cater for this error in the class? i.e. can i make a class robust enough to elegantly handle not getting the variables passed in to it that it expects? Hope this is clear enough.
captainmerton is offline  
Reply With Quote
Old 08-26-2009, 03:53 PM   #4 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 170
Thanks: 8
captainmerton is on a distinguished road
Default

Here's an example:

My class:
PHP Code:
class SimpleClass
{
    public function 
__contruct($variable1,$variable2) {
        do 
some stuff with variables 1 and 2;
    }

My client code:

PHP Code:
$newobject = New SimpleClass($variable1); 
This errors and the script stops executing because the class hasnt received the 2 variables required to instantiate the object. I have this option (which i dont want to do):

PHP Code:
if ((isset(variable1)) && (isset($variable2))) {
    
$newobject = New SimpleClass($variable);

Is there a way I could handle this more elegantly? i.e. i want the class to be robust enough to handle idiots passing in junk data. I assume that is the point of object orientation.
captainmerton is offline  
Reply With Quote
Old 08-26-2009, 04:22 PM   #5 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

...
public function __contruct($variable1,$variable2=0) {
...
or if a string...
public function __contruct($variable1,$variable='') {

Is that what you mean?
__________________
JaoudeStudios.com | JaoudeStudios.com Forum | JaoudeStudios.com Blog
OpenSource is the road ahead...!
JaoudeStudios is offline  
Reply With Quote
Old 08-26-2009, 04:28 PM   #6 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 170
Thanks: 8
captainmerton is on a distinguished road
Default

I dont want to hard code the variable no.

I'll try and make it more simple:

I have created a class (a stand alone component of functionality). I have no control over what goes into that class all I know is i need 2 pieces of information to instantiate the object. I want to build in some kind of validation into the class this is there a way I can check for existance of the 2 variables inside the class?

I dunno i feel i am just talking round in circles. Either i validate the variables outside of the class and make sure they are in existence before i pass them in to the class or I do it inside the class. I'd rather find a way of doing it inside the class otherwise it isnt a very robust class if the whole thing falls over when it get unexpected input.
captainmerton is offline  
Reply With Quote
Old 08-27-2009, 07:58 AM   #7 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

Yes definitely do it in the class, keep it all contained and encapsulated.

The code I gave you is not hard coding a value as such, as it refers to a default option, making the 2nd variable optional from outside the class! So you do not need to pass the 2nd variable when initiating the class object, however you will need to check in the class if another value besides the default has been passed.

Maybe we are not on the same page?!?!
__________________
JaoudeStudios.com | JaoudeStudios.com Forum | JaoudeStudios.com Blog
OpenSource is the road ahead...!
JaoudeStudios is offline  
Reply With Quote
The Following User Says Thank You to JaoudeStudios For This Useful Post:
captainmerton (08-27-2009)
Old 08-27-2009, 09:50 AM   #8 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 170
Thanks: 8
captainmerton is on a distinguished road
Default

No we are on the same page apologies i misunderstood you. I think what you are suggesting is exactly what i'm looking for. I can set default values for both variables as spaces in the class and throw an exception if either has a value of spaces when the class is called. If I pass in the variables when calling the class this will override the default values set in the class.

Next question - is throwing an exception in a constructor method bad practice as it leaves a half instantiated object?

Many Thanks.
captainmerton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
base classes..... allworknoplay Absolute Beginners 16 05-10-2009 09:09 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 03:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 02:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 05:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 05:23 PM


All times are GMT. The time now is 07:12 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design