I'm trying to use more OOP as I've typically always done procedural coding.

Right now, I've writing a page which will search a database based on some inputs and display a list of results.

In short, I have a page called cars.php within this page I have sliders to look at cost etc, it then uses AJAX to call get-cars-search.php

Within get-cars-search.php I have my SQL query and a loop for all results..

I guess my question is, should each result be defined as a new instance of an object 'Car' for example, or can I just echo them onto the page with some styling wrapped around it?

What's 'best practice' ??

while($row = $stmt->fetch(PDO::FETCH_OBJ)) {

var_dump($row);

}

    To my mind, if any object you create is not going to be interacted with as something that is a collection of data and methods that act upon that data, then you're just talking about syntax -- whether you use $row['id'] or $row->id, which may have style benefits, but is not really OOP.

    If you truly want to get all OOP about it, then yes, you might define a Car class, and you might have a Cars class that contains a collection of Car objects, and that Cars class might implement the Iterable interface to make it relatively easy to loop over for your display needs.

    Or not. 🙂

      Write a Reply...