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