Several people have told me learning OOP would improve my websites, so I read a couple beginner’s tutorials. It wasn’t as theoretically complex as I expected, but it’s still a little over my head. It would help if I could understand how it relates to my code. One author stressed that OOP and procedural PHP both have their place, and OOP should come into play only if your procedural code becomes too complex or confusing.
I have a CMS - not a brand name but one that I created myself. It’s a actually a collection of websites rolled into one. That is every site is built on PHP includes stores in the home website. Every website has multiple sections, some shared with other websites. For example, most have World and Reference sections - MySite/World, MySite2/World, etc.
So I have variables that display the names of nations ($MyCountry = United States), their parents ($MyParent = North America) and abbreviations ($MyID = U.S., $IDParent = NA). I also have variables that describe types of pages (e.g. $MyKind = Home, World Home or Child, $MyKind2 further subdivides it into children of child pages).
It gets a little confusing, but I can’t understand how a more complex code (OOP) would make it any simpler.
The code for my reference pages can get really complex, with lots of variables for details. But, again, I don’t see how OOP would be an improvement.
One thing that would help, though, is if I could somehow condense some of my longer, more complex PHP switches. For example, the code I posted below is a series of switches designed to classify living things into various categories - animals vs plants, vertebrates vs invertebrates; vertebrates into mammals, birds, etc.; warm-blooded vs cold-blooded vertebrates, etc.
Is that something that could be improved with OOP? If so, how would I start? Would I designate each major group a class? For example, this is from the tutorial:
class MyClass
{
public $prop1 = "I'm a class property!";
}
$obj = new MyClass;
var_dump($obj);
echo $obj->prop1;
Would I change that to something like this?
class Mammals
{
public $prop1 = "I'm a mammal!";
}
$obj = new Mammals;
var_dump($obj);
echo $obj->prop1;
…and then I’d somehow append smaller groups of mammals (e.g. primates, whales) to that? It looks to me like OOP would actually make my switches more complicated, but I’m probably missing something. Thanks.
Note: Don’t bother analyzing the code I posted below; I just wanted to illustrated how long and complex it is. The starting values (e.g. $Kingdom) derive from a database query that defines parents, grandparents, great-grandparents, etc.
switch($Kingdom)
{
case 'Animalia':
// Endo/Ectotherms...
// INNER SWITCH
switch($MyCla)
{
case 'Mam':
case 'Ave':
$Therm = 'endotherms';
break;
default:
$Therm = 'ectotherms';
break;
}
// END INNER SWITCH
// Define $MyCla2...
// INNER SWITCH
switch($Tetrafish)
{
case 'Fish':
// INNER SWITCH 2
switch($MyCla)
{
case 'Act':
$MyCla2 = 'Act';
break;
default:
$MyCla2 = 'Fishetc';
break;
}
// END INNER SWITCH 2
break;
default:
// INNER SWITCH 2
switch($MyCla)
{
case 'Rep':
case 'Amp':
$MyCla2 = 'Herps';
break;
default:
$MyCla2 = $MyCla;
break;
}
// END INNER SWITCH 2
break;
}
// VERTEBRATE vs INVERTEBRATE...
// INNER SWITCH
switch($Vert)
{
case 'Vert':
// INNER SWITCH 2
switch($Tetrafish)
{
case 'Tetrapod':
// Define $Herptile...
// INNER SWITCH 3
switch($MyCla)
{
// Herptiles...
case 'Rep':
case 'Amp':
$Herptile = 'herptiles';
break;
default:
$Herptile = '';
break;
}
// END INNER SWITCH 3
// INNER SWITCH 2
// SUBORDERS
switch($MyCla)
{
case 'Mam':
// INNER SWITCH 3
switch($MyOrd)
{
case 'Carnivora':
// INNER SWITCH 4
switch($MyFam)
{
case 'Odobenidae':
case 'Otariidae':
case 'Phocidae':
$Suborder = 'Pinnipedia';
break;
default:
$Suborder = '';
break;
}
// END INNER SWITCH 4
break;
case 'Artiodactyla':
// INNER SWITCH 4
switch($MyFam)
{
case 'Camelidae':
$Suborder = 'Tylopoda';
break;
case 'Hippopotamidae':
case 'Suidae':
case 'Tayassuidae':
$Suborder = 'Suina';
break;
default:
$Suborder = 'Ruminantia';
break;
}
// END INNER SWITCH 4
break;
case 'Cetacea':
// INNER SWITCH 4
switch($MyFam)
{
case 'Balaenidae':
case 'Balaenopteridae':
case 'Neobalaenidae':
$Suborder = 'Mysticetes';
break;
default:
$Suborder = 'Odontocetes';
break;
}
// END INNER SWITCH 4
break;
default:
break;
}
// END INNER SWITCH 3
break;
case 'Rep':
// INNER SWITCH 3
switch($MyOrd)
{
case 'Squamata':
// INNER SWITCH 4
switch($MyFam)
{
case 'Acrochordidae':
case 'Aniliidae':
case 'Anomalepidae':
case 'Anomochilidae':
case 'Atractaspididae':
case 'Boidae':
case 'Bolyeridae':
case 'Colubridae':
case 'Cylindrophiidae':
case 'Elapidae':
case 'Hydrophiidae':
case 'Leptotyphlopidae':
case 'Loxocemidae':
case 'Tropidophiidae':
case 'Typhlopidae':
case 'Uropeltidae':
case 'Viperidae':
case 'Xenopeltidae':
// Snakes
$Suborder = 'Serpentes';
break;
case 'Amphisbaenia':
case 'Amphisbaenidae':
case 'Rhineuridae':
// Worm lizards
$Suborder = 'Amphisbaenia';
break;
default:
// Legless lizards: Anniellidae
// Lizard families: Amphisbaenia, Anguidae, Agamidae, Anniellidae, Chamaeleonidae, Cordylidae, Corytophanidae, Crotaphytidae, Dibamidae, Gekkonidae, Gerrhosauridae, Gymnophthalmidae, Helodermatidae, Hoplocercidae, Iguanidae, Lacertidae, Lanthanotidae, Opluridae, Phrynosomatidae, Polychrotidae, Pygopodidae, Scincidae, Teiidae, Tropiduridae, Varanidae, Xantusiidae and Xenosaurida
$Suborder = 'Lacertilia';
break;
}
// END INNER SWITCH 4
break;
default:
break;
}
// END INNER SWITCH 3
break;
default:
break;
}
// END INNER SWITCH 2
// End Tetrapod Suborders...
break;
case 'Fish':
// Define $Fish2...
// INNER SWITCH 3
switch($MyCla)
{
case 'Act':
case 'Sar':
$Fish2 = 'Bony';
break;
case 'Ela':
case 'Hol':
$Fish2 = 'Cartilage';
break;
default:
$Fish2 = 'Jawless';
break;
}
// END INNER SWITCH 3
// SUPERORDERS
// Sharks & Rays
// INNER SWITCH 3
switch($MyCla)
{
case 'Ela':
// INNER SWITCH 4
switch($MyOrd)
{
case 'Pristiformes':
case 'Rajiformes':
case 'Torpediniformes':
$Superorder = 'Batoidea';
$Superorder2 = 'rays';
break;
default:
$Superorder = 'Selachimorpha';
$Superorder2 = 'sharks';
break;
}
// END INNER SWITCH 4
break;
default:
break;
}
// END INNER SWITCH
break;
default:
break;
}
// END INNER SWITCH 2
break;
case 'Invert':
break;
default:
break;
}
// END INNER SWITCH
break;
case 'Plantae':
break;
default:
break;
}
// FISH
switch($Tetrafish)
{
case 'Fish':
break;
default:
break;
}
// FISH iFamilies
switch($MyCla)
{
case 'Act':
// INNER SWITCH
switch($MyOrd)
{
case 'Scorpaeniformes':
$iFam = 'Scorp';
break;
case 'Siluriformes':
$iFam = 'Silur';
break;
case 'Perciformes':
// INNER SWITCH 2
$iPerci = substr($MyFam,0,1);
switch($iPerci)
{
case 'A':
case 'B':
case 'C':
$iFam = 'Perci1';
break;
case 'D':
case 'E':
case 'G':
case 'H':
case 'I':
case 'K':
case 'L':
$iFam = 'Perci2';
break;
case 'M':
case 'N':
case 'O':
case 'P':
case 'R':
$iFam = 'Perci3';
break;
case 'S':
case 'T':
case 'U':
case 'X':
case 'Z':
$iFam = 'Perci4';
break;
default:
break;
}
// END INNER SWITCH
break;
case '':
$iFam = 'ActMisc';
break;
break;
default:
break;
}
// END INNER SWITCH
break;
default:
break;
}
// Divide mammals into nine groups...
switch($MyCla)
{
case 'Mam':
// INNER SWITCH
switch($MyOrd)
{
// Marine Mammals
case 'Cetacea':
case 'Sirenia':
$Duende = 'Marine_Mammals';
break;
// Bats
case 'Chiroptera':
$Duende = 'Bats';
break;
// Ungulates
case 'Proboscidea':
case 'Perissodactyla':
case 'Artiodactyla':
$Duende = 'Ungulates';
break;
// Carnivorans
case 'Carnivora':
$Duende = 'Carnivorans';
break;
// Primates
case 'Primates':
$Duende = 'Primates';
break;
// Rodents
case 'Rodentia':
$Duende = 'Rodents';
break;
// Marsupials
case 'MARSUPIALS':
$Duende = 'Marsupials';
break;
// Tropical Oddballs
case '':
$Duende = 'Oddballs';
break;
// Misc.
default:
break;
}
// END INNER SWITCH
break;
default:
break;
}
switch($MyURL)
{
case 'Mammals':
case 'Marine_Mammals':
case 'Bats':
case 'Ungulates':
case 'Carnivorans':
case 'Primates2':
case 'Marsupials':
case 'Tropical_Oddballs':
case 'Other_Mammals':
$MyCla = 'Mam';
break;
default:
break;
}