TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   How to cater for missing input to a class (http://www.talkphp.com/absolute-beginners/4897-how-cater-missing-input-class.html)

captainmerton 08-26-2009 12:21 PM

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?

JaoudeStudios 08-26-2009 02:37 PM

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?

captainmerton 08-26-2009 02:44 PM

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 08-26-2009 02:53 PM

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.

JaoudeStudios 08-26-2009 03:22 PM

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

Is that what you mean?

captainmerton 08-26-2009 03:28 PM

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.

JaoudeStudios 08-27-2009 06:58 AM

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?!?!

captainmerton 08-27-2009 08:50 AM

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.


All times are GMT. The time now is 10:11 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0