mausie Posted May 8, 2008 Share Posted May 8, 2008 Hi all, I’ve got a question about OOP Classes and the best way to use ‘em. Mostly I use classes like car. $renault = new car(); Then you can Set values, Update the values to db with internal functions like $renault->setBrand. But I also want to get all cars from the db with a function to list ‘em all on my site. Should I create 2 classes then? Car and cars? (cars->getAllCars) And what if I have a class cars to use getAllCars. Is there a easy to to convert all received data into a Array with objects which are member of the class car? A other way is just the class cars with all functions like updateCar($id, $brand, etc). Im really wondering what’s the best way to build my applications! Thanks, Maurice Quote Link to comment https://forums.phpfreaks.com/topic/104671-best-way-of-classes/ Share on other sites More sharing options...
ionik Posted May 8, 2008 Share Posted May 8, 2008 Take a jump into MVC...if you really want to start using the most power out of classes in PHP and what you are doing here can all be done in one class...just define seperate functions for what you need to do Quote Link to comment https://forums.phpfreaks.com/topic/104671-best-way-of-classes/#findComment-535923 Share on other sites More sharing options...
Mastodont Posted May 9, 2008 Share Posted May 9, 2008 Should I create 2 classes then? Car and cars? (cars->getAllCars) Yes, it is common way of doing things in OOP. Class Cars is collection. You could write other methods in this class, i.e. cars->findCar cars->deleteCar cars->addCar ... Quote Link to comment https://forums.phpfreaks.com/topic/104671-best-way-of-classes/#findComment-536500 Share on other sites More sharing options...
mausie Posted May 9, 2008 Author Share Posted May 9, 2008 Ok so cars holds all common functions. And car will be used for car objects (Set values etc) and then the update/del functions from car refer to the functions in cars. Right? Sounds very logic! I assume cars will be a static class then? Or should i create a object and pass it to the constructor of car? Quote Link to comment https://forums.phpfreaks.com/topic/104671-best-way-of-classes/#findComment-536537 Share on other sites More sharing options...
deadonarrival Posted May 19, 2008 Share Posted May 19, 2008 Static classes are usually to be avoided. It's usually better to make a singleton class (a class with only one instantiation) or use a registry if you find you're using too many singletons. so in each "car" you use $cars = cars::getinstance(); to get the instance of car. That way you don't have to worry about passing it to every method you need, or the constructor. It's debated - but I'd prefer to use the constructor of car to get the instance of "cars", rather than passing it as an argument to allow you to only change it in the constructor, rather than whenever you instantiate a "car" object. Quote Link to comment https://forums.phpfreaks.com/topic/104671-best-way-of-classes/#findComment-544966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.