-1

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?

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, so ultimately it just makes it more readable and better to manage when using OOP in web development. For my current project, I've been including this PHP file in all my other web files. Since the PHP variables can be used throughout the file, I would just call them when I need them. For example, if I wanted to display the image, I'd just say echo "<img src='/media/assets/$avatar>"; and the image is displayed. I don't understand the necessary need for making functions to return these variables, which is why I'm having a hard time implementing OOP in my code
If I created a class full of functions related to manipulating and displaying user data, and included this class file to every page of my site, wouldn't performance decrease because of the added extra code? Let's say I'd like to display only the user's name on a certain page and I added that class file, wouldn't the class file be an overhead if I only wanted to retrieve the user's name?
Well OOP isn't the solution for everything, and for some things it probably would sacrifice performance for efficiency, in the same way that you wouldn't write 5 of almost the same function because they would individually run faster than one that has additional logic inside it.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.