Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy use set_include_path() in PHP and how to autoload classes from different folders?
    primarykey
    data
    text
    <p>I have a couple questions about the include path in PHP and including files and/or classes. </p> <p>Below is a simple snippet of code where we are setting multiple include paths. I saw this in another project but I am not sure why? </p> <p>I have never had to use <strong>set_include_path()</strong> in any of my projects over the last 5 years or so. </p> <p><strong>Q1)</strong><br> So what exactly is the purpose of setting an include path? I have always just included the path in my include() call. </p> <p><strong>Q2)</strong><br> In the example below it sets more then 1 path. How does this work for including files in multiple locations, i'm confused on the purpose or what it does exactly? </p> <pre><code>&lt;?php // Define App path define('APPLICATION_PATH', realpath('../')); // Build array of 3 different paths $paths = array( APPLICATION_PATH, APPLICATION_PATH . '\com', get_include_path() ); /* Result of array above... Array ( [0] =&gt; E:\Web Server\xampp\htdocs\test [1] =&gt; E:\Web Server\xampp\htdocs\test\com [2] =&gt; .;C:\php5\pear ) */ // Set include path from array above // http://us3.php.net/manual/en/function.set-include-path.php set_include_path(implode(PATH_SEPARATOR, $paths)); ?&gt; </code></pre> <p><strong>Q3)</strong><br> This is slightly different question but still relates to includes. Below is a simple autoload function for classes. I used to have a classes folder and autoload ALL my class files. In my current project, I have a library of classes to autoload like below does, but I then also have another section where I might need to autoload class files from a modules directory. </p> <p>So I will need to autoload my <strong>library classes</strong> located somewhere like this.... </p> <p><strong>root/includes/library/classes/library_class_files.php</strong> </p> <p>+++plus+++ </p> <p>load classes for different modules (account, messages, friends, photos,blogs, forums, etc) located somewhere like this.... </p> <p><strong>root/modules/forums/modules_class_files.php</strong> </p> <p>I might not need to loads class files from the 2 different locations, but if I do, how would I go about doing that? </p> <pre><code>&lt;?php //auto include class files that we need on a per page basis function __autoload($class_name){ include('library/classes/' .$class_name . '.class.php'); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload