Skip to content

Building a website with OOP and reusable classes

Bicycle51 is a concept website that I built after following a tutorial on OOP. What hooked me on this one was having a database class that could serve any CRUD - this one being selling used bicycles. To extend this or…

Comment
Building a website with OOP and reusable classes

From the archive: this post is about the code behind the first steveshead.io, written in 2021-22. The site runs on Xavier now, so the code here is history rather than how things work today.

Bicycle51 is a concept website that I built after following a tutorial on OOP. What hooked me on this one was having a database class that could serve any CRUD - this one being selling used bicycles. To extend this or change this to sell used cars (for instance) you would just need to create a cars class, essentially mimicking the bicyle class. Obviously there's a bit more to that, but it makes the code very reusable - something that has a lot of value for me - and is part of the essence of OOP. Once I got the hang of it I realized the value of using classes, and started to extend what I was learning. I might even try to create a new site reusing the database class for something else.

There are other classes also. There is a pagination class, admin class and session class. When reusing the code those don't need to change - again, that makes creating a new site quite simple. This site was also built using the concept of having a private folder and public folder. All classes, functions, configurations live in the private folder, and all of the front end files live in the public folder. I'm stating the obvious here - but - you then point the site root at the public folder. My take is, the next level of working this way is going the MVC route - not sure I'm ready for that yet.

There are always a couple of sides to doing this. One is the PHP & MySQL that drives the site, the other is designing the website itself. I dabble in both. I use Github and have quite a few repositories for website designs, and quite a few that are private repositories for apps that I have built along the way. My CSS framework of choice is Bootstrap (currently version 5) since it makes framing out a site simple. That being said, I create CSS classes to extend that functionality - some of which I reuse. I also try to use SCSS such that adding or changing things like colors and spacing becomes simple.

Anyway - this post isn't going to be as long as the first post, but I wanted to give a bit of flavor on OOP and show an example of what can be built using that route. The picture at the head of the post shows some of the front end, and the picture below shows the admin panel dashboard. I used the SB Admin 2 dashboard theme from Startbootstrap for the dashboard - love the way it turned out.

bicycle admin panel

I hope some of this was interesting. My journey through PHP coding is intensifying and I'm enjoying the ride. Some if it is becoming intuitive such that I don't need to think about what I'm doing, and finding solutions to issues along the way is also becoming easier - that feels good.

Enough for now. As always, please let me know your thoughts and ideas.

Happy coding!

Cheers

~Steve

Comments

No comments yet - start the conversation.

Code

Creating a car class that extends the database class

This post is a follow up to the second post, creating a website built using OOP, using reusable classes! I decided to see what it would take to extend that site by creating another class - this time around I created a…

Code

Adding draft posts: it's a story!

This past weekend I added the functionality to view draft posts. I know that should have been a thing earlier on but I was thinking about it wrong, and unless it's clear in my mind I can't continue. Let me explain. I…

Code

Adding 'save as draft' to creating posts

When I built the backend functionality of this website I built the ability to create posts, but didn't add the ability to save a post as a draft. I honestly didn't think that would be necessary. I was wrong! I also…