Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to structure factories, namespaces, and classes with an autoloader in PHP 5.3?
    primarykey
    data
    text
    <p>Sorry for the lengthy explanation... I like to be thorough.</p> <p>I am very new to OOP PHP (about a week of coding in it so far) but I have caught on pretty well and I really like it! I had placed all of my classes into one GIANT include file and I was including it in every page that needed an object that was being stored in the <code>SESSION</code>. Then today I realized that this was difficult to navigate and that every page was loading every class even if it didn't use it so when I read somewhere that it was best to break classes out into their own files I put all of them in their own files inside a lib folder.</p> <p>The student class went in the student.php file. the staff class went into the staff.php file. etc. etc.</p> <p>Here is where I get confused... I programed a few factories for looking up certain information from a database. These factories work great! I pass the type (such as ID, username, asset) and the factory returns the appropriate lookup which then can be passed the criteria.</p> <p>Here is an example of what I did for one factory. The file name is devicelookup.php</p> <pre><code>&lt;?php // Device Lookup /* * device lookup interface. This ensures that each lookup provider has the same function */ interface deviceLookupProvider { public function lookupDevice($arg); } /* * class assetLookupDevice */ class assetLookupDevice implements deviceLookupProvider { public function lookupDevice($arg) { global $mysqli; $asset = $mysqli-&gt;real_escape_string($arg); $sql = "SELECT computer.* FROM `computer` WHERE computer.assetTag = '" . $asset . "' LIMIT 1;"; $result = $mysqli-&gt;query($sql) or die($mysqli-&gt;error); if ($result-&gt;num_rows &gt; 0) { return $result-&gt;fetch_assoc(); } else { return null; } } } /* * device lookup factory. This returns a lookup object based off the type. */ class DeviceLookupFactory { public static function createDeviceLookup($type) { switch ($type) { case "asset": return new assetLookupDevice; break; } } } </code></pre> <p>I am trying to implement the spl_autoloader using code on php.net like so:</p> <pre><code>function my_autoloader($class) { include 'lib/' . $class . '.php'; } spl_autoload_register('my_autoloader'); </code></pre> <p>This kind of function would work great for student, staff, workflow and the other classes that are the same name as their files but I feel strange about breaking apart the factory and it's implementations into their own files within /lib.</p> <p>If I have to do this kind of break apart then I want them grouped into their own folders or something and then I ran into namespaces. I sort of understand the concept of putting a namespace on an individual class... but I don't.</p> <p>And, if I figure out the best structure for namespacing then I should be able to call </p> <pre><code>&lt;?php spl_autoload_extensions(".php"); // comma-separated list spl_autoload_register(); ?&gt; </code></pre> <p>to just magically include all the class files that are needed as they are needed.</p> <p>So... how does one structure this environment? I want to structure everything properly so I can reuse the object coding for the larger application that isn't OOP in the future.</p>
    singulars
    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.
 

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