View Full Version : Resolved OOP subclass conditions
ggalan
07-17-2011, 03:00 PM
I have read many times that good OOP practice is to avoid conditions such as
if($a>$b){
if($a1<$b1){
if($a2<$b2){
//code here
}
}
}else if($b>$c){
if($b2>$c2){
if($b3>$c3){
//code here
}
}
}
and you should subclass when you have messy code like this. but i havent been able to find exactly how to do this. can anyone show an example of replacing conditions with subclass please
well, the ideal is to abstract out the way you deal with the code, and thereby avoid deeply nested blocks like this. for example (paraphrased from a current project of mine):
if(!empty($_GET['page'])){
if(preg_match('/^[A-Za-z0-9]+$/',$_GET['page'])){
$result = mysql_query("SELECT * FROM `pages` WHERE `page`='{$_GET['page']}' LIMIT 1");
if(mysql_num_rows($result) === 1){
/* do stuff with results */
}else{ /* error: page not found */ }
}else{ /* error: page request not valid */ }
}else{ /* error: no page requested */ }this gets difficult to deal with very quickly. It can be abstracted out something like this:
class page{
public function __construct(){
if(!empty($_GET['page'])){ $this->checkPage($_GET['page']); }
else{ $this->noPageRequested(); }
}
public function checkPage($page){
if(preg_match('/^[A-Za-z0-9]+$/',$page)){ $this->findPage($page); }
else{ $this->invalidPageRequest($page); }
}
public function findPage($page){
$result = mysql_query("SELECT * FROM `pages` WHERE `page`='{$_GET['page']}' LIMIT 1");
if(mysql_num_rows($result) === 1){ $this->doStuffWithResults($result); }
else{ $this->pageNotFound($page); }
}
public function doStuffWithResults($results){
/* do stuff with results */
}
/* other functions, etc. */
}
this is an over-simplified example, but i hope it shows how it can make your code far cleaner, more organized, and manageable.
djr33
07-17-2011, 10:40 PM
Generally, think about your programming as doing things, rather than running through lines of code. This also helps if you want to reuse anything later. It's similar to using basic functions to simplify code but more powerful.
ggalan
07-18-2011, 02:44 AM
i see
traq, so from your example invalidPageRequest and pageNotFound would be other methods in this class, correct?
right (I just didn't write everything out, I think you get the idea)
ggalan
07-18-2011, 01:48 PM
if the inheritance chain gets large and you have to change the top, initial method how do you deal with all the other methods? wouldnt this be similar to messy code?
it depends on what sort of changes you need to make - if you need to completely re-write something, then it will be messy no matter what. But if you just need to change the logic/procedure, and you don't need to change the lower-level functions (as is often the case), then it's much cleaner.
There's no actual "inheritance" (I don't think that's what you're really referring to) in the above example. Inheritance comes into play when you have classes that "extend" a parent class.
Also, as Daniel said, one of the main advantages of OO code is that it is much more reusable - you don't have to repeat blocks of code that are used in several places in the script.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.