Building blocks

So I figured I’d actually get in here and ask, just because my brain is trying to come to terms with oop.

Let me start off with, I haven’t a clue. But I think that I can explain it in a way that kinda sorta makes sense. Not going to be actual code, just something to read and follow along.

I take a idea, write it out, then write it in code. Used to do this for procedural so it makes sense to keep at least that much.
My brain is telling me that I need to write out a class in sort of a template fashion, ie functions, arrays, statements, if/else.

So I might have something like this, please elaborate if I’m wrong:
Class User
Function displayProfile()
if session(isset){
mysql query = select all from users where email=?
tableData arrays for name, email,whatever data
else session(empty){
go try again
} return ← do I need to use return at the end of a function inside a class?

Well, in my opinion there really shouldn’t be no if then conditions (or a bare bare minimum) in a table that you retrieve as the query should handle that part of the method/function.

Here’s an example of what I’m trying to say →

    // Display Record(s) by Pagination
    public function page($perPage, $offset, $page = "index", $category = "general"): array
        $sql = 'SELECT * FROM '. $this->table . ' WHERE page =:page AND category =:category ORDER BY id ASC, date_added DESC LIMIT :perPage OFFSET :blogOffset';
        $stmt = $this->pdo->prepare($sql); // Prepare the query:
        $stmt->execute(['page' => $page, 'perPage' => $perPage, 'category' => $category, 'blogOffset' => $offset]); // Execute the query with the supplied data:
        return $stmt->fetchAll(PDO::FETCH_ASSOC);

The query ($sql) handles what is tor be retrieved and the $stmt->fetchALL() puts every record into an array and return the data. You want methods(functions) to be as simplest as possible and classes that are name for what they should do with in the code. That goes for JavaScript classes as well.

What I try to do is write a class with methods that will save me a lot of repetition and the benefit of classes is once you write them they are easily copied into the next project with minimum modifications to the classes (if at all).

Sponsor our Newsletter | Privacy Policy | Terms of Service