0

Okay, let's say I two files and a MySQL table called Users and a table called Select.

  • index.php
  • fucntions.php

Users

ID    NAME    L_NAME     POINTS

0     Jill    Jackson     10
1     Jack    Wills       90
2     John    Doe         30

Select

ID    OPTION

0     Points
1     Name
2     LastName   

index.php contains a simple table and select like so.

<?php include 'output.php'; ?>

<h3>Sort by: </h3>

<select>
  <?php getSelect(); ?>
</select>

<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Points</th>
  </tr>
  <tr>
  <?php outputData(); ?>
  </tr>
</table>

My fucntions.php file has the following code:

function outputData() {
    try {
        $handler = new PDO("mysql:host=$host;dbname=$db", "$user", "$pass");
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e)
     {
        echo $e->getMessage();
        echo 'Sorry, an error has occurred.';
        die();
     }

    $query = $handler->query('SELECT * FROM `Users`');

    while($r = $query->fetch(PDO::FETCH_OBJ)) {
    echo
        '
            <td>
                <input type="text" value="'.$r->Name.'" >
            </td>
            <td>
                <input type="text" value="'.$r->l_name.'">
            </td>
            <td>
                <input type="text" value="'.$r->points.'">
            </td>
        ';
    }
}

function getSelect() {
    try {
        $handler = new PDO("mysql:host=$host;dbname=$db", "$user", "$pass");
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e)
     {
        echo $e->getMessage();
        echo 'Sorry, an error has occurred.';
        die();
     }

    $query = $handler->query('SELECT * FROM `Select`');

    while($r = $query->fetch(PDO::FETCH_OBJ)) {
    echo
        '
            <li>'.$r->option.'</li>
        ';
    }
}

I would be using this code to output data onto multiple different pages. This is just a very basic example... I have multiple different functions for outputting and inserting data from tables. My question is, would using classes be a better option than just writing bare functions like I have here and including it on every page I need?

What are the benefits of using a class vs what I'm currently doing?

I have looked at various tutorials of how to construct classes but I've never actually found the real need as to what the benefit is when compared to what I'm doing currently...

Could someone elaborate?

ProEvilz
  • 5,310
  • 9
  • 44
  • 74
  • Sorry, will correct now... coded all that on the fly. – ProEvilz Jan 14 '15 at 20:04
  • there are no benefits to using OOP, the way you're going. You're connecting to the DB in both method calls, which is pointless. Since you're not using any OOP precepts (e.g. data encapsulation), you're basicaly doing more OBP - object BASED programming. – Marc B Jan 14 '15 at 20:05
  • 3
    "and a table called Select." Why torture yourself like that? Don't name tables with words that are reserved as part of SQL itself. Or you could make a buncch of tables named "Select", "Where", "Group By", "Sort By", "Delete", "Update", and "Create Table". – developerwjk Jan 14 '15 at 20:05
  • 2
    It's not my litteral code! It's I wrote that as an example... – ProEvilz Jan 14 '15 at 20:06
  • possible duplicate http://stackoverflow.com/questions/2122123/when-to-use-a-class-vs-function-in-php – Zach Spencer Jan 14 '15 at 20:06
  • 1
    Not a duplicate at all.. – ProEvilz Jan 14 '15 at 20:07
  • If you're looking for a better way of doing this sort of thing, consider that modern PHP development encourages the use of a [development framework](http://codegeekz.com/best-php-frameworks-for-developers/) like [Laravel](http://laravel.com/) that gives you a solid foundation for building your application. Find one that suits your style and needs and follow the examples and documentation. Most have a large library of community code you can add in with little effort, avoiding the need to reinvent the wheel. – tadman Jan 14 '15 at 20:31

1 Answers1

1

To me the main need for using classes, at least in the scenario you gave, is to help to

  • the readability of the code. Code is much more readable using objects that represent something in the real life, also, you don't mess all the functions into one or several files. If a function affects one class (f.i. uses its class members, despite this is very relative), you know that that class.php is where you should create that function and where you should find it.

  • the maintainability of the code. For the same reason, code is much more maintainable. You know what an object is supposed to do, you read easily what your code should do and where. This helps, imho debugging. Also, when you are programming you in a way more intuitive know which data goes into a class and which method of that object which contains that data can be used.

  • encapsulation. This concept entails more effort when programming because you have to take care about which members should be accessible by other instances of other classes or main code but can prevent lots of mistakes. For instance,could prevent cases where you are debugging and you expect a value for a variable which doesn't correspond with what really is happening... and all because there is a piece of code somewhere which changed that value and you didn't even realize.

  • reusability. Your classes could be useful in other projects with a few changes (and easy to locate where and what you need to change)

For sure there should be many more reasons, but those are the main coming to my head. BTW, using procedural programming can be easier, faster and even cleaner in small projects or parts of the project.

javier_domenech
  • 5,995
  • 6
  • 37
  • 59