3 Replies - 401 Views - Last Post: 24 May 2019 - 11:28 AM Rate Topic: -----

#1 total212   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 19

Understanding the OOP PHP

Posted 23 May 2019 - 10:35 AM

I would like to see if I could get some info about how the OOP works, this "mental matrix" of this (I already understand the procedural model). I mean there is classes and we create objects from them. These classes have properties and methods. Two or more objects made from the same class can have different values (unless they are static, so these dont change if I got this correctly). There is also Public, Private and Protected and we also have the inheritance. So I guess this would be it, but I have no idea how this would overlap with $_POST, $_GET, $_SESSION, $_COOKIE, conditionals, arrays (would these be the properties?), loops? We have input (request) - process (my code) - output (response) and I cant imagine how the OOP coding work on these (as I can do for the procedural code). Basically how I could understand the OOP based on already understanding how the procedural coding works. I know that we make one class per file and we also use namespaces in the OOP, so these are the layers of this too. Just need to click into this "mental matrix", not there yet.

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding the OOP PHP

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2331
  • View blog
  • Posts: 7,107
  • Joined: 15-January 14

Re: Understanding the OOP PHP

Posted 23 May 2019 - 10:50 AM

Quote

So I guess this would be it, but I have no idea how this would overlap with $_POST, $_GET, $_SESSION, $_COOKIE, conditionals, arrays (would these be the properties?), loops?

None of that changes.

It's just a different way to structure your application. If you're handling logged-in users and what they could do, you could have a global function for each thing, a function to check if someone is logged in, a function to log them in or out, a function to get their fields, update their fields, etc, whatever actions you want users to do. Or, you can make a user class and put all of those functions in there as class methods. It just keeps things cleaner.

Inheritance is another reason. Instead of making one set of functions for users and another set for admins, you could make an abstract user class that applies to all users, and extend that class to make another class for "regular" users with everything that only they can do, and extend the abstract class with one for admin users that contains things only they can do. It eliminates things like copying and pasting code all over the place and it keeps the global scope clean because you don't have a lot of functions that may have names in common with something else you decide to include later. It's also pretty likely that you'll be able to move most of your classes to another application later without changing many things about them if you design it correctly.

It doesn't change at all how you use any of the control structures in PHP. There are also no rules regarding how many things you can have in one file. It just makes sense to have a single class defined in a single file because if you want to edit that class you know exactly where to look and there's nothing in that file except what you're looking for.
Was This Post Helpful? 1
  • +
  • -

#3 total212   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 19

Re: Understanding the OOP PHP

Posted 23 May 2019 - 10:37 PM

Thanks a lot for the info.

1) We mix OOP code with procedural code so it is not just the OOP coding and all the most important syntax elements of the PHP mixed into that in some way (like $_POST or $_GET inside classes only as properties). I use OOP but it gets mixed with the procedural coding as well... I am assuming this is correct...

2) Do I need to or would it be better to put all the functions inside classes or can I have procedural style php functions in the mix as well. I mean, I guess I can, but how would this work. No, put all functions as methods inside of classes?

3) I am not sure how I would create a list of classes for something like a cms, lets say (wanting to focus on just one type of thing that I want to work on or start with, instead of thinking about tube, blog, forum, which could be additional things that I would need to understand. Would I base it on already made html/css code (final) or (final) database design? Like classes are really based of what you have in the database, like tables - maybe I am not getting something like this. I mean, what are these mental attachment points for figuring this out. Also, how would I know what properties would I create in there and also methods. I guess I can figure it out by trying and so on, but maybe there is some rules of this or "mental attachment points". Basically CSM, lets say good blog, "2020", how would I come up with a list of classes and then know what properties and methods are in there. Like you start on the top, like you have admins and users so you make one class for this and the other will be inheriting (like it was mentioned in the reply)?

4) Based on the above, I guess this can be flexible, it can be either admins and users class or just one and inheriting, so no all people would do it the same way... So there is this flexibility too here, it can be this, this or that...

Thanks again for any info that I could get, I will be sharing info with other people to when I get to a better level of this.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2331
  • View blog
  • Posts: 7,107
  • Joined: 15-January 14

Re: Understanding the OOP PHP

Posted 24 May 2019 - 11:28 AM

Quote

Do I need to or would it be better to put all the functions inside classes or can I have procedural style php functions in the mix as well. I mean, I guess I can, but how would this work. No, put all functions as methods inside of classes?

It's up to you, if you have a bunch of related functions that would make sense to go together, then make them part of a class. If you have a bunch of functions that deal with manipulating images, for example, then make some sort of image class and make them all methods. It's up to you how to organize everything so that it makes sense. Do it with the mindset that you should be able to copy your class with minimal changes to a new application. If you would have to make a bunch of changes to your code to use them in another application then it's probably not organized very well.

Quote

Like classes are really based of what you have in the database, like tables - maybe I am not getting something like this.

Often they are, yeah. Tables often represent objects in your application, like a user, or a forum topic, or a forum post, etc, and those map to classes fairly well.

Quote

I mean, what are these mental attachment points for figuring this out.

The most important one is that it should make sense, it should be logical. Everything in one class should be related to that class, and in general the classes should be as small as possible. Don't make a huge forum class that handles everything possible about a forum. Make a class for a forum user, another for a single forum post, another for a forum topic, then a category, then the forum itself. Separate things into distinct parts and then put them back together in your code. If someone makes a post then you create a user object for that person, you create a new post object and pass it the user object as the author, you either create a new topic object or get the existing topic and give it the new post object, etc. In each of those objects is probably some code that interacts with the database to make the actual database changes.

Quote

Basically CSM, lets say good blog, "2020", how would I come up with a list of classes and then know what properties and methods are in there.

Experience. You learn how to do things like this with experience, and you get experience by trying things and failing.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1