0

I've got a doubt with instances in PHP. I've created one class that basically sends queries to a database. The question is that it's correct to create an instance everytime I want to execute a query or just use the same instance overriding the memory allocated to that class. For example:

//Select
$conversation = new mQuery();
$conversation->operation = "select";
$conversation->data = array("bla", "bla", "bla");
$result = $conversation->execute();

//Update
$conversation = new mQuery();
$conversation->operation = "update";
$conversation->data = array("bla", "bla", "bla");
$result = $conversation->execute();

VERSUS

//Create instance ONCE
$conversation = new mQuery();

//Select
$conversation->operation = "select";
$conversation->data = array("bla", "bla", "bla");
$result = $conversation->execute();

//Update
$conversation->operation = "update";
$conversation->data = array("bla", "bla", "bla");
$result = $conversation->execute();

Thanks in advance.

ProtectedVoid
  • 1,293
  • 3
  • 17
  • 42
  • Usually we create new object for each operation specially if the object is just for that. Also it make the code cleaner and easier to understand. The language should handle the garbage collection for you – Hani Jul 13 '16 at 21:28
  • 1
    It also depends on the implementation of the class. What if you forget to call `->data` and it reuses the data from the previous run? – FirstOne Jul 13 '16 at 21:28
  • @FirstOne That's why I'm in doubt. Maybe it's better to create a new instance everytime to clean the memory, so that if there's a wrong variable it will fail instead of taking it from the previous assignment. – ProtectedVoid Jul 13 '16 at 21:33
  • You are hardly going to need to worry about that in the question.. I doubt you are going to run such an amount of queries that would cause you trouble. (Still, questioning that is valid). – FirstOne Jul 13 '16 at 21:34
  • What you could do is, once you are done with the object, [enable it to be destroyed](http://stackoverflow.com/questions/8798443/best-way-to-destroy-php-object). Then, just create a new object every time you need to run a query (first option from the question). – FirstOne Jul 13 '16 at 21:38

1 Answers1

0

Seems like a good re-usable class that mQuery, I would recommend going for the second option since the class and it's method seem like utility methods and nothing significant from the previous query is stored in the instance.

coderodour
  • 1,072
  • 8
  • 16
  • _"[...] and nothing significant from the previous query is stored in the instance."_ if you consider the type of the query and it's data _not significant_... (See what I mean in [this comment](https://stackoverflow.com/questions/38361918/php-classes-and-instances#comment64136374_38361918)) – FirstOne Jul 13 '16 at 21:50
  • @FirstOne From the structure provided by the OP, it seems pretty obvious they know to call the data and its already in the `$result`. Beside, forgetting a line of code can happen anywhere and can lead to all kinds of discrepancies. That is incorrect code, a mistake and chosing one method over another will not always keep you safe from that. – coderodour Jul 13 '16 at 22:07
  • Don't worry lol, I haven't downvoted your answer... I just think that a code that doesn't work is better than a code that works but could produce _unwated_ results (like using the previous data). peeace... – FirstOne Jul 13 '16 at 22:18