php - need help with logic

zamrg

Senior Member
Joined
Oct 19, 2005
Messages
804
Reaction score
11
Location
Cape Town
I haven't really done too much OOP in php5 but I've had to start on a recent project and would like to take advantage of it.

I have a database table, for sake of simplicity lets say it's a table of categories which is filled with the following data: Dog, Cat, Mouse and Sheep. The app has an abstract class Animal and 4 child classes which extend it; Dog, Cat, Mouse and Sheep. The parent and child classes all have the same constructor parameters. The script then reads each category from the database and instantiates the child class based on the category name.

eg: (pseudocode)
read categories from database
foreach category {
if category = Dog, return new Dog('arg1', 'arg2', 'arg3', etc)
if category = Cat, return new Cat('arg1', 'arg2', 'arg3', etc)
if category = Mouse, return new Mouse('arg1', 'arg2', 'arg3', etc)
if category = Sheep, return new Sheep('arg1', 'arg2', 'arg3', etc)
...etc, etc
}

What would be the simplest and best way to instantiate the child classes based on the category name?

I've tested a switch statement, but it looks stupid and repetitive since their are 10+- categories an therefore 10+- child classes. I've also tried php's newish reflection methods, but this feels a bit erky.

any suggestions?
 
Not 100% sure of this, but...

function x($var){

$newClass = new $var();

}
 
Cool beans.

Do you use a framework at all? QCodo (PHP5) was good, but is floundering and now forked.
 
I don't, but thats cause I've never really looked into them and taken the time to study the API, it's almost like learning the language all over again :)

I've heard good things about CodeIgniter before, is it any good?

Are frameworks really that beneficial as opposed to using 'lower' level coding?
 
Using CodeIgniter at the moment. It's good, though only PHP4. Kohana is the PHP5 fork, but the people maintaining it seem a bit... juvenile.

Using a framework definitely saves loads of time, I would recommend it.
 
I don't, but thats cause I've never really looked into them and taken the time to study the API, it's almost like learning the language all over again :)

I've heard good things about CodeIgniter before, is it any good?

Are frameworks really that beneficial as opposed to using 'lower' level coding?

Depends on the problem you are solving, amongst other things. I liked Symfony when I was doing PHP coding. Keep in mind though that if you are looking for a single project solution the learning curve of most of these frameworks is probably not worth it.
 
Just going OT with the frame work offshoot;

Zend has a very lively and very active base, has awesome components so you can make a nice transition into it. Zend DB and Zend form are awesome. For me this is the top gun in the world of frame works.

Symfony is good, can be resource heavy with out optimistion, older versions are coupled but with the new versions is getting better and better. This also has an active base so you can get plenty help.

CakePHP has a nice active base but it is more of PHP trying to become rails.

Frameworks are awesome, firstly they can do just about anything you want, what they can't do you can always extend yourself. They are also written in such a way that they better your code. You become a better programmer because you have to in order do what you want with the code.
 
I don't like dicking around in console windows so Cake was a flop (har har).
 
You may also want to look into dependency injection, never tried it in PHP but it should work. It's probably too complex for this example though.
 
Top
Sign up to the MyBroadband newsletter
X