Bytes
Bytes

Reputation: 721

Using OOP in PHP with retrieving data from query

I have a page that retrieves user data and displays that data on the page. My code works perfectly fine, but I am having trouble understanding when to use OOP for web development. My current page is very simple, and my understanding is that OOP consumes a lot of processing power for creating objects. Should a simple page like this require the use of OOP?

<?php
    session_start();
    require $_SERVER['DOCUMENT_ROOT'] . "/connection.php";
    if ($_SESSION["id"] != ""){
        $result = $db->query("
            SELECT `Username`, `Image`, `FirstName`, `LastName`, `Type`
            FROM Users WHERE `id` = '{$_SESSION['id']}'
        ");
        if ($result->num_rows == 0){
            $switch = false;
        } else {
            $resultSet = $result->fetch_array();
            $avatar = $resultSet["Image"];
            $account_type = $resultSet["Type"];
            $username = $resultSet["Username"];
            $fName = $resultSet["FirstName"];
            $lName = $resultSet["LastName"];
            $id = $_SESSION["id"];
            $switch = true;
        }
    }
?>

Afterwards, the information is displayed in the HTML. For example, when I want to display the name I just echo it out. Like so echo "<p>$fName</p>"; should this approach be changed in to an objected oriented approach instead? Or is my design suitable as it is?

Upvotes: 0

Views: 1346

Answers (2)

Maytch
Maytch

Reputation: 71

I guess you could say OOP becomes more desirable once your code becomes larger, but starting with OOP is always a good option for scaleability and easy to understand code.

What you have there is basically a file with a function inside it, and if you wanted to retrieve something else which relates to users, you'd probably create another file with another function in it, calling similar things. What you could have is something like this:

class User {
    private $id;
    private $username;
    private $firstName;
    private $lastName;
    private $type;
    private $image;

    function __construct($id) {
        // TODO SQL stuff

        $this->id = $id;
        $this->username = // blahblah
    }
}

And it could have all sorts of functions (known as methods in a class), and it'll all be tucked away in a separate User class. When you or someone else is writing bits of code to the project, they don't need to know the inner code of User, they just need to know that your method returns X when Y is put into it, and their bit of code can just be:

$user = new User($id);
return $user->doSomeMethod();

If you wanted to get the full name of the user later on, you wouldn't need to make a separate file that does the same SQL stuff, you'd just add this inside your User class (and add some comments to it):

/**
*   Returns the proper case of the user's first and last names
*   @return String - full name of user
*/
public function getFullName() {
    return ucwords($this->firstName . " " . $this->lastName);
}

Then you can just call getFullName() on your User object in the rest of your code. You don't want to have bits of code relating to the same subject (users) spread out in different areas, it makes it harder to work with.

As for processing power I'm not too sure, but if it helps to keep your code DRY (don't repeat yourself) and easy to understand, it's a good idea to use OOP.

Upvotes: 1

BadAddy
BadAddy

Reputation: 376

perhaps person preference, but I would not use any resource to duplicate variables.

I would use the response $resultSet. To use the variable I would:

echo $resultSet["LastName"];

If it was imperative to use variables then perhaps use an associative array, and use a loop:

foreach ($resultSet AS $k => $v {
    ${$k} = $v;
}

Depending on the name of the column in the database would then become the variable name.

Upvotes: 1

Related Questions