-2

I am new to OOP and how it works. I am having trouble figuring this out.

  • I have a page called general.php which displays a members code, name etc
  • I get the member's code using $_GET['code'] from the url.
  • I get the member's other details from a database using the member's code through my CRUD class.
  • I have a members class which contains properties such as code, name etc.

Should I create a new members object and assign the name to its name property or do I just get the data from the database and display it on the page? The object dies when I go to another page unless I put it into a session variable but I saw on other sites that it is not a good idea to do that.

I am struggling to understand the point of creating an unique members class object for all members in this case and not just a general members class object to call its functions when needed?

Hope I explained this okay.

bass71982
  • 21
  • 1

1 Answers1

2

You use objects when they make your job easier.

In this case - get an ID, show a name - I don't think they'll gain you very much.

A couple of screens further along in your application - when you're creating new users and taking lots of data items and validating and storing them - that's when objects start to show their worth because you can start to bind that functionality together, making it reusable in [lots of] other places.
"Write once, [re]use often."

BTW, taking the user's ID from the QueryString arguments probably isn't a good idea. If I were using your web page and I were "curious", I might try typing in other, different values for the ID and, thereby, getting shown other people's names (and other data). This would be a serious breach of Data Privacy, which might get you into some very serious [legal] trouble.

It's a bit scary, but see: What technical details should a programmer of a web application consider before making the site public?

Phill W.
  • 11,891
  • 4
  • 21
  • 36
  • *"You use objects when they make your job easier."* - this sentence is absolutelly great! too many devs are locked in single paradigm world - use object for everything! or use functions, functional programming is the only way! or everything should be a thread, parallel programming is the future! those are tools not religions, tools only. we as a developers are free to choose and use them **when they make our job easier**. – rsm Mar 01 '17 at 15:54