March 3, 201016 yr Hey. It's been several few years now since I started with PHP and for the past 6+ months I've been progressively programming in OOP. I recently bought a book called "Clean code - A Handbook of Agile software craftmanship" to help me improve my OOP. I try to stick to using classes that cover ONE area of functionality and methods that only do ONE thing. It would be great if you could cast an eye over this code and let me know if I'm on the right track. It's basically the control page for the app. The app is a RESTful currency converter which grabs the latest currency rates on a new request and outputs an XML result. I've only got so far as the error handling as yet. <?php include ('inputValidator.class.php'); include ('errorHandler.class.php'); include ('XML_Handler.class.php'); include ('currencyConverter.class.php'); /* Create new validator and pass in the query string from the browser */ $validator = new inputValidator($_SERVER['QUERY_STRING']); // If the query string contains errors if ($validator->hasErrors()) { // Create new errorHandler, passing in error codes from validator $myErrors = new errorHandler($validator->getErrorCodes()); /* Create a new XML_Handler and pass in the error data from the error handler */ $myXML = new XML_Handler($myErrors->getError()); // Set headers // XML content type header('Content-Type:text/xml'); // HTTP response code header($myErrors->getHeader()); // Print the XML echo $myXML->getErrorXML(); } else { // No errors - Handle request /* uncommented as this is not yet implemented */ $converter = new currencyConverter(); $converter->getLatestRates(); $converter->doConversion($_SERVER['QUERY_STRING']); $myXML = new XML_Handler($converter->getResult()); echo $myXML->getResultXML(); } ?> I have a few queries when it comes to calling $converter->doConversion() and alike. Should the class do this it's self without a call or am I doing the right thing by calling the doConversion(). I could do this differently by making the currencyConverter() class __construct call getLatestRates() & doConversion then return the XML so it would be done as soon as I created a new instance. Another question.. I could have also created just ONE class for this entire app, containing all of the appropriate methods. Would this have been a reasonable way to do it? Thanks, Dave
March 3, 201016 yr Hi Dave. The method you have used is perfectly acceptable. The rule of thumb with objects is that you want them to be reusable and as flexible as possible without giving the programmer a headache implementing it, so you need to balance it quite carefully. With the method you've used of passing the query string you've allowed your class to be used with other input such as a text string defined by something other than the server. You could also employ the __construct method at this juncture, which would be perfectly acceptable if that's its primary purpose (and just pass all arguments from the construct to the doConversion() function, therefore ensuring the DRY technique As for the one class to do al this, it's best not to create overly complicated objects, unless you're doing the same thing again and again and can modify parameters to meet your goals with the same class
March 3, 201016 yr This is a common problem - and you generally look at making a Factory class for this kind of thing. In a nutshell, you don't want to pepper your constructor with loads of method calls if you can help it. Instead, make a class that builds the class and calls the methods, then returns the class post setup. i.e. you could use a switch class Factory{ public function build($classname){ switch($classname){ $class = new $classname() //various object setup stuff break; } return $class; } } or you could use some magic public function build($classname){ if(method_exists($this,$classname){ return $this->$classname(); } } private function XMLthingy(){ //setup an XML thingy } I would also allow for a second argument in the factory call to pass in some options to the class it builds.
March 3, 201016 yr Author Thanks for your replies guys. sunwukung, what is the common problem? Sorry I'm a bit slow today. How could I used your examples to improve my controller page? Thanks, Dave
March 3, 201016 yr Author Hi Dave. The method you have used is perfectly acceptable. The rule of thumb with objects is that you want them to be reusable and as flexible as possible without giving the programmer a headache implementing it, so you need to balance it quite carefully. With the method you've used of passing the query string you've allowed your class to be used with other input such as a text string defined by something other than the server. You could also employ the __construct method at this juncture, which would be perfectly acceptable if that's its primary purpose (and just pass all arguments from the construct to the doConversion() function, therefore ensuring the DRY technique As for the one class to do al this, it's best not to create overly complicated objects, unless you're doing the same thing again and again and can modify parameters to meet your goals with the same class Reading over your post again I see what you mean with regards to DRY. I repeated the passing of $_SERVER['QUERY_STRING'].
March 3, 201016 yr Thanks for your replies guys. sunwukung, what is the common problem? Sorry I'm a bit slow today. How could I used your examples to improve my controller page? Thanks, Dave In your initial post, you talked about putting the various steps into the constructor. Object creation can become a messy business, creating and calling several methods one after the other - so generally speaking, this is what the Factory pattern is for. It enable you to tuck those successive calls into one method in your Factory - i.e. $V = Factory::build('Validator',$errors); //returns an object that has gone through the setup you showed in your initial post $V->report();//echo out the errors As to the second part of your question, no, generally you should not build a God class - each class should be responsible for one particular job. http://en.wikipedia.org/wiki/God_object
March 3, 201016 yr Author In your initial post, you talked about putting the various steps into the constructor. Object creation can become a messy business, creating and calling several methods one after the other - so generally speaking, this is what the Factory pattern is for. It enable you to tuck those successive calls into one method in your Factory - i.e. $Xml = Factory::build('Xml',$errors); $Xml->report();//echo out the errors As to the second part of your question, no, generally you should not build a God class - each class should be responsible for one particular job. http://en.wikipedia.org/wiki/God_object Ace cheers buddy!
Create an account or sign in to comment