Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP OOP PHP Classes

Tmillard.15

New Coder
I am currently completing a project for my A Level Computer Science, and have been using OOP in PHP. My project/desired result is to design an 'UberEats' style website, with a recommendation algorithm that lists restaurants based on previous preferences.

I'm slightly concerned that I only have 4 classes - these are html (for rendering html), form (for creating and manipulating form elements), database (for all database transactions, and binding values to SQL syntax) and JavaScript (for rendering/scripting the interactive elements of the site). Is this bad practice? Should I really have a 'class user' if all my user data is stored in an extensive relational database?

Thanks for your advice
 
It probably makes sense to have a user class that calls your database class. For instance, you could have a method in your user class called getEmail() that returns the user's current email address by querying the database using a query that calls a method in the database class to execute the query.
 
I am currently completing a project for my A Level Computer Science, and have been using OOP in PHP. My project/desired result is to design an 'UberEats' style website, with a recommendation algorithm that lists restaurants based on previous preferences.

I'm slightly concerned that I only have 4 classes - these are html (for rendering html), form (for creating and manipulating form elements), database (for all database transactions, and binding values to SQL syntax) and JavaScript (for rendering/scripting the interactive elements of the site). Is this bad practice? Should I really have a 'class user' if all my user data is stored in an extensive relational database?

Thanks for your advice

I would set it up like this...

1. System / Database class
2. User class, extends System/Database
3. Employee class, extends User (not needed for smaller project - just put all in User) - for drivers
4. Customer class, extends User (not needed for smaller project - just put all in User)
5. Vendor class, extends User (or make its own) - for restaurants, etc
6. Orders class, for holding pending orders, etc
7. Invoice class, for charging Customers, and then paying Employees & Vendors - extends Orders

Really it's kind of funny because you could come up with like 20+ new class files easily for an UberEats platform... Or you could code the thing in a minimal amount of files. It really depends on how fast you want to make it, if you want it organized, and if you want to reuse the code for other stuff in the future. You'll notice that none of my class recommendations really relate to UberEats because I personally will only ever write code that is reuseable.

I use classes for things like that rather than having a class for HTML or JS. Or if I do it's usually a view routing or constructor that I use in a lot of projects with generic functions that work for anything rather than having a view class specific to 1 project.

Pretty much all of your data is going ot be in an extensive relational database, or some sort of database. The class file doesn't replace that or change that, it just gives a set way to get that data.

For example...
You'd have your User class designed to insert/update/select/delete data from your users. Your User class would extend the System/Database class so it would have all the utility from there. In general, if you have a Database class file it should literally be completely unrelated to your project - It should be able to be copied and pasted for a new project, have the db details changed, and work the same way because it's a class file for database functions, not anything specific to any single project.
So if you're not going to have a generic database class file and a User class, the only alternative would be to use procedural functions and inline SQL to access your database. Or you could make your database class file do things specific for users, but at that point you might as well call your class file "Everythingin1file.php" because it's not technically a "database" class if it actually is full of code specific to things IN the database. Think about it like the difference between having a book about cooking in general versus having a book about only chicken. Your User class is the chicken, an extension of cooking in general, while the Database class is the generic stuff that applies to all projects, not just this one.
 

New Threads

Buy us a coffee!

Back
Top Bottom