Jump to content

PHP Classes & Data


coxdabd

Recommended Posts

Hi all, I'm struggling with a bit of logic here. What's the score on connection to a database inside of a class (PDO). I'm guessing it's bad practice and any queries should be passed as an array to the object - is this correct?

 

What should I do with entering data into the DB? For example, say you have an orders class, one of the methods is to create the order, would you return an array from the method of all the data to be entered into the DB and then to enter back in the script?

 

Getting pretty good at OOP, just need a little extra help. Have had a play with a framework (CodeIgnitor) but found it too limiting if that makes any sense - although I did like the MVC pattern.

Link to comment
Share on other sites

Can you try to clarify more about ur problem means are you telling that u have order form which user fills in and once it is fill u will create and array of all the data that user and filled in and will pass that array to the class function and insert it into the database than i say,

 

It is bad practice to create a array after the data is submitted since the u will get the data in post as an array format and again creating new array not good practice

Link to comment
Share on other sites

Hey dude, apologies that was just an example - a bad one. What I'm trying to ask, is say you have an Order class for example, say one of the methods is to to view a particular order in the DB, would I put the SQL inside the method or feed the SQL to it through the script through a parameter - is connecting to a database inside a class bad practice?

Link to comment
Share on other sites

If you are familiar enough with MVC you will understand that data is represented by Models. Your 'Order' class should represent a record from within your 'Orders' database table.

 

It is fine for a Model such as this to depend on a 'Database' class. But this class should implement a well defined interface and should be passed to the Model via dependency injection of some variety.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.