Search code examples
phpoopooad

php object oriented programming


I have some questions pertaining OOP. I'm not an advanced user and I'm facing a problem which some may have experience before.

Basically I'm using MVC to work with my php projects. I tried to make the class as independent as possible. But I think this is rather tough. e.g:

I've got a class Car and User. User posts car info to a website, maybe to sell...

In class User, I have getUser basically to get user info.

So in class Car, I may have setCar, getCar, saveCar. To display these cars, I may have a displayCars(). in displayCars(), I may use getCar to get the list of Cars. but I may also need to use User::getUser to get the user information of users who have submitted those car. So in a way, displayCars is dependent on User::getUser.

So what should I do here?


Solution

  • You need more than just Cars and Users to model this problem. Conceptually, I would at least have the following:

    • Each individual car should be represented by a Car object
    • Users input information that populates the attributes of the Car object
    • Both the Car object and the user that submitted that Car become attributes of a CarDBItem object
    • All of the CarDBItems are then placed into a CarDB object which manages all of the cars that any of the users have input

    Of course, this is heavily simplified -- there will be a lot more to it than this when you actually write the application. But I think that looking at the problem in these terms is a good place to start.