<p>I would like to have a reference for the pros and cons of using include <strong>files vs objects(classes)</strong> when developing PHP applications.</p>

<p>I know I would benefit from having one place to go for this answer...I have a few opinions of my own but I look forward to hearing others.</p>

<p><strong>A Simple Example:</strong></p>

<p>Certain pages on my site are only accessible to logged in users. I have two options for implementation (there are others but let's limit it to these two)</p>

<ol>
<li><p><em>Create an authenticate.php file and include it on every page. It holds the logic for authentication.</em></p></li>
<li><p><em>Create a user object, which has an authenticate function, reference the object for authentication on every page.</em> </p></li>
</ol>

<p><strong>Edit</strong>
I'd like to see some way weigh the benefits of one over the other. 
My current (and weak reasons) follow:</p>

<p>Includes - Sometimes a function is just easier/shorter/faster to call
Objects - Grouping of functionality and properties leads for longer term maintenance.</p>

<p><strong>Includes</strong> - Less code to write (no constructor, no class syntax) call me lazy but this is true.</p>

<p><strong>Objects</strong> - Force formality and a single approach to functions and creation. </p>

<p><strong>Includes</strong> - Easier for a novice to deal with
Objects - Harder for novices, but frowned upon by professionals.</p>

<p>I look at these factors at the start of a project to decide if I want to do includes or objects.
Those are a few pros and cons off the top of my head.</p>

[{'answer_id': 22532, 'author': 'Thomas Owens', 'author_id': 572, 'author_profile': 'https://Stackoverflow.com/users/572', 'pm_score': 1, 'selected': False, 'text': "<p>I don't have much PHP experience, although I'm using it at my current job. In general, I find that larger systems benefit from the readability and understandability that OO provides. But things like consistency (don't mix OO and non-OO) and your personal preferences (although only really on personal projects) are also important.</p>\n"}, {'answer_id': 22537, 'author': 'Huppie', 'author_id': 1830, 'author_profile': 'https://Stackoverflow.com/users/1830', 'pm_score': 0, 'selected': False, 'text': '<p>Can you be a bit more specific? For the example you give you need to use include in both ways.\nIn case 1 you only include a file, in case 2 you need to include the class file (for instance user.class.php) to allow instantiation of the User class.</p>\n\n<p>It depends how the rest of the application is built, is it OO? Use OO.</p>\n'}, {'answer_id': 22551, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 1, 'selected': False, 'text': "<p>I've learned never to use <code>include</code> in PHP except inside the core libraries that I use and one central <code>include</code> of these libraries (+ config) in the application. Everything else is handled by a global <code>__autoload</code> handler that can be configured to recognize the different classes needed. This can be done easily using appropriate naming conventions for the classes.</p>\n\n<p>This is not only flexible but also quite efficient and keeps the architecture clean.</p>\n"}, {'answer_id': 22578, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 0, 'selected': False, 'text': "<p>Whether you do it in classes or in a more procedural style, you simply need to check to ensure that:</p>\n\n<ol>\n<li>There is a session;</li>\n<li>That the session is valid; and,</li>\n<li>That the user in possession of the session has proper privileges.</li>\n</ol>\n\n<p>You can encapsulate all three steps into one function (or a static method in a Session class might work). Try this:</p>\n\n<pre><code>class Session\n{\n  const GUEST = 0;\n  const SUBSCRIBER = 1;\n  const ADMINISTRATOR = 2;\n\n  public static function Type()\n  {\n    session_start();\n\n    // Depending on how you use sessions on\n    // your site, you might just check for the\n    // existence of PHPSESSID. If you track\n    // every visitor with sessions, however, you\n    // might want to assign some separate unique\n    // number (that you can track in a DB) to\n    // authenticated sessions\n    if(!$_SESSION['uniqid'])\n    {\n      return Session::GUEST;\n    }\n    else\n    {\n      // For the best security, don't store the\n      // user's access permissions in the $_SESSION,\n      // but rather check against the DB. This will\n      // ensure that recently deleted or downgraded\n      // administrators will not be able to make use\n      // of a previous session.\n\n      return THE_ACCESS_LEVEL_ACCORDING_TO_THE_DB\n    }\n  } \n}\n\n\n// In your files that need to check for authentication (you\n// could also do this in a controller if you're going MVC\n\nif(!(Session::Type() == Session::ADMINISTRATOR))\n{\n  // Redirect them to wherever you want them to go instead,\n  // like a log in page or something like that.\n}\n</code></pre>\n"}, {'answer_id': 22587, 'author': 'Christian Lescuyer', 'author_id': 341, 'author_profile': 'https://Stackoverflow.com/users/341', 'pm_score': 5, 'selected': True, 'text': '<p>These are not really opposite choices. You will have to include the checking code anyway. I read your question as procedural programming vs. OO programming.</p>\n\n<p>Writing a few lines of code, or a function, and including it in your page header was how things were done in PHP3 or PHP4. It\'s simple, it works (that\'s how we did it in <a href="http://www.oscommerce.com/" rel="noreferrer">osCommerce</a>, for example, an eCommerce PHP application).</p>\n\n<p>But it\'s not easy to maintain and modify, as many developers can confirm.</p>\n\n<p>In PHP5 you\'d write a user object which will carry its own data and methods for authentication. Your code will be clearer and easier to maintain as everything having to do with users and authentication will be concentrated in a single place.</p>\n'}, {'answer_id': 22594, 'author': 'Karim', 'author_id': 2494, 'author_profile': 'https://Stackoverflow.com/users/2494', 'pm_score': 3, 'selected': False, 'text': "<p>While the question touches on a couple of very debatable issues (OOP, User authentication) I'll skip by those and second Konrad's comment about __autoload.  Anyone who knows C/C++ knows how much of a pain including files can be.  With autoload, a PHP5 addition, if you choose to use OOP (which I do almost exclusively) you only need use some standard file naming convention and (I would recommend) restricting a single class per file and PHP will do the rest for you.  Cleans up the code and you no longer have to worry about remembering to remove includes that are no longer necessary (one of the many problems with includes).</p>\n"}]
['https://Stackoverflow.com/questions/22528', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2490/']