Jump to content

Best way of classes


mausie

Recommended Posts

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

 

 

Link to comment
Share on other sites

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  8)

 

 

Link to comment
Share on other sites

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

...

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.