dcsimg
Results 1 to 2 of 2

Thread: How to write code the OOP way

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2019
    Posts
    2

    How to write code the OOP way

    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?

    Thanks.

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,529

    Re: How to write code the OOP way

    You are confusing Object Oriented Programming with Routing and Model-View-Controller style coding.

    Your project can still be 100% OOP while being individual files (login.php, home.php, process-pics.php, etc).

    You can use routing to hide your file structure and give your URLs a "cleaner" look. https://mydomain.com/index.php?page=login or if you use mod_rewrite (on apache) your URLs can look like https://mydomain.com/login

    Model-View-Controller (MVC) is where you separate your View (HTML, CSS, JS) from your Logic (Controller) and from database interactions (Model). This adds a new layer of complication to your project but also improves readability and workability. If you have a large team working on a project, the designers can work on the view file while the programmers work on the controller file.

    Take a look at Laravel. Create a small project with this to learn how to separate all the components of your project.
    My usual boring signature: Something

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width