Jump to content

Advantages of using classes rather than includes?


littlened

Recommended Posts

I'm just getting into classes, and wondered what the advantages were of using classes over include files?

So when when I've developing something in php, I've created a php file which contains the HTML for the page, with <?= $var; ?> in places where I wanted data to be echo'd. At the top of the file I had a config include which set the database connection and site title etc, and then each page had it's own seperate php file which is included at the top of the page. The include file simply did any mysql queries and built up an array of the data, or it saves data to a database which was completed by a form.

example.

[code]
<?php
include('php/php_config.php'); // Includes config file
include('php/php_view_details.php'); // gets data from DB

<html>
<body>
<?= $details_from_database; ?>
</body>
</html
?>
[/code]

the contents of the php_view_details.php file would be

[code]
<?php
$sql = "SELECT DestID,DestAirports,DestDesc FROM Search_Destinations ORDER BY DestOrder ASC";
$sql_result = mysql_query($sql,$database) or die(mysql_error());
while ($row = mysql_fetch_array($sql_result)) {
$details_from_database = $row['whatever'];
}
?>
[/code]

now using includes, I'm more or less doing the same thing, including a class file, and then running code from a function. So I see that I'll have less files because everything will be in a class, and that I'll be able to include certain classes and use the same function for other pages of the site that require it.

Also, lets say I create a class to get data from a database, do I return the data and then format it how I want it, do I get the class to format it, or do I get the data from the database, return it, then pass the data to another class to be formated?

Sorry for all the questions, it's just I've never saw any advantage in using classes so I've never used them, now I feel I should use them because its the proper way.
Link to comment
Share on other sites

This is a bit like comparing apples to oranges really. they are two completely different things. You use includes to include classes. Yes you can use classes to simply wrap common functionality but there is allot more two them than just that.

Classes are designed to describe data. For instance, you may have a class called user.

[code]
<?php
  class user {
    private $name;
    private $age;
    private $email;
    function __construct($id) {
      // connect to database and retrieve users data.
      $row = mysql_query("SELECT name,age,email FROM users");
      $this->name = $row['name'];
      $this->age  = $row['age'];
      $this->email = $row['email'];
    }

    function getname() {
      return $this->name;
    }

    function getage() {
      return $this->age;
    }

    function getemail() {
      return $this->name;
    }
  }
?>
[/code]

As you can see... this class describes a user. Includes have NOTHING to do with this sort of functionality.

As for this question....

[quote]do I return the data and then format it how I want it, do I get the class to format it[/quote]

IMO it should nearly allways be up to the calling code to do the formatting. Unless of course your class is specifically designed to do the formatting and in that case it should not be responsible for retrieving the data. Make your classes as to the point as possible, as soon as your class starts getting away from the data it is meant to describe you need to start looking at moving the extra functionality into another class.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.