Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

How to write code the OOP way

5:43 am on May 29, 2019 (gmt 0)

Junior Member

5+ Year Member

joined:Apr 23, 2014
votes: 0

As far as I know I redirect all requests to an entry point, something like a domain.com/index.php and then this gets redirected to a desired sub-page (if needed). I know the theory of the OOP quite well (like all the syntax elements and how they work) and I know that I would create one file per class (not a necessity but a good way to go).

As related to the above:

a) Where to I actually write code, I mean I create objects from classes, but where does it take place like in Linux filesystem (like I would have domain.com/process-pics.php and I would code there). Where does it go in the OOP way?

b) Do all conditional statements and loops go into objects (I mean, I write this code inside them). I know that I put $_POST and probably other superglobals in objects, so just wondering if my goal would be to have like 100% of the code inside of objects or if there would be cases where I just add it in a procedural way. Just have max amount of code inside of objects or not necessarily? If it is inside and outside (outside being procedural in the mix) what are the rules of this?

4:44 pm on May 29, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
votes: 5

There are a lot of ways to structurally form your application. The most common of them is the MVC (Model, View, Controller) paradigm.

The cliffs notes are basically this. You have 2 basic "types" of classes. Models and Controllers, between them they handle a user request, fetch/write data and pass results of to a View

The model classes make up your objects, they are defined by your model classes and you store the objects they create in a DB but can be stored as flat files.

An example of objects you would use models classes for would be a things like User object, a Message object, and a way to relate them to say Users can have messages. Model Classes should have factory methods that support basic CRUD functionality (Create, Read, Update, Delete)

The Controller Classes are what deal with HTTP requests. For example you would have a User controller, if someone requests the their user profile page the controllers job is to get authenticate the request, retrieve all the data required from the Models and pass it on to a View.

This is a very basic overview and doesn't take into account things like routing http request to methods within your controllers.

As to where you put things it is kind of up to you. You will want to make use of Namespaces though to easily load models and make use of inheriting from base classes.

A basic structure *could* look like this


I think it is very good to understand these things and how they work, but starting from the ground up is a lot of work. I would really recommend checking out some frameworks. I highly endorse Laravel but there are others out there as well. Laravel will boilerplate the entire MVC structure for you and you can really hit the ground running as far as developing what you want to build goes.

Check it out, do some reading about MVC, come back with questions. I think this is one of the most important things for PHP developers to learn if they want to be successful and have lean development times.
5:40 am on May 30, 2019 (gmt 0)

Junior Member

5+ Year Member

joined:Apr 23, 2014
votes: 0

Thanks a lot for the info. 1.5 year of edu already done and the "procedural matrix of things already figured out"; I now need to bite into the OOP. I started reading Matt Zandstra - "Objects, patterns and practice" but it seemed like it could have been too hard. For this reason, I've been wondering how to bite into this - basically whether I should just get into a framework (people recommended me Symfony so I would like to do that, seems like a more solid thing, the best / #1). I also have an OOP / MVC course from Udemy (I have not started on this yet) and some other OOP courses but they are more like "public / private / protected / properties / methods" instead of teaching about how things get together as a whole (which is what I was trying to find out here). So I've been thinking:

a) figure this out from the Net (like here, and again thanks for the info as this is really a lot of help)
b) do the courses that I have
c) read Matt Zandstra
d) do the Symfony 4

I am not sure if this order is correct, maybe I could just start from Symfony 4 and then do Matt Zandstra or do like both at the same time.

I have quite a lot of this initial info so this will enable me to research further from the Net too. So probably this, courses, Matt Zandstra / Symfony 4 as the last steps.
4:26 pm on June 3, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 15, 2003
votes: 5

If it were me I would dig right in. I am not sure how your brain works but mine gets confused after too much theory and no practice.

Maybe pick a really easy assignment for yourself and try to build it out. Something simple with basic CRUD functionality.

A form that takes in data and re displays it somewhere else, with the ability to edit/delete

Something like "A guy has a booth at a comic convention and wants to track people's favorite comic book heroes and villains."

Make him a form people can input their favorites and have the results display to a "leader board" for the top fan picks. You could jump right into Sym 4 this way and then when doing your courses and reading Matt's book the context will make more sense. Again that is just how my brain works but rolling up my sleeves and jumping in the thick of it has always helped me make sense of the theories being introduced.
8:38 pm on June 3, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member tangor is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 29, 2005
votes: 967

Theory is great. Practical application is where you KNOW that theory!

Code something ... even mistakes are educational.