Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Autoloading
    primarykey
    data
    text
    <p>I've been looking around quite a bit and can't find a concise answer to this question anywhere- perhaps those of you with more experience autoloading than myself could shed some light. Pretty simple question:</p> <p>I wrote a simple <code>autoloader</code>:</p> <pre><code>function __autoload($className){ $className = BASEDIR . "include/class/" . str_replace("_", "/", $className) . ".php"; require_once($className); } </code></pre> <p>Where <code>BASEDIR</code> is the base project directory. Works great including classes- go figure. However, when I go to include classes that extend other classes, it starts to break.</p> <p>Per the suggestions on this site, I'm using <code>PEAR</code> naming conventions for my classes. So my "Event" class (describing an actual event like a party) lives here:</p> <pre><code>include/class/Event/Event.php </code></pre> <p>The actual class name is "Event".</p> <p>When autoloading the class, I reference it like so:</p> <pre><code>new Event_Event(); </code></pre> <p>The autoloader turns that into: </p> <pre><code>"include/class/Event/Event.php"; </code></pre> <p>The problem comes when <code>Event</code> is extended by some other class utilizing this naming convention. I'm using, for example:</p> <pre><code>class EventInstructed extends Event_Event </code></pre> <p>The autoloader knows where to look, but since my Event class name is "Event" and not "Event_Event", it is trying to look for the wrong class and can't find it.</p> <p>The question therefore is, how do I utilize the pear naming conventions correctly, or should I do extra parsing in my autoloader to fix my issue? Explode the string on underscores, grab the class name and use that instead?</p> <p>Naming conventions are here: <a href="http://pear.php.net/manual/en/standards.naming.php" rel="nofollow">http://pear.php.net/manual/en/standards.naming.php</a></p> <p>Thanks!!</p> <p><strong>edit</strong></p> <p>My final solution was as mentioned in the accepted answer. Classes are in the hierarchy as such:</p> <pre><code>include/class/Event/Event.php include/class/Event/Lesson.php // &lt;-- extends Event </code></pre> <p>Inside the Event file:</p> <pre><code>abstract class Event_Event{ //Class code.... } </code></pre> <p>Inside the Lesson file:</p> <pre><code>class Event_Lesson extends Event_Event{ //Class code.... } </code></pre> <p>And finally, the __autoload function:</p> <pre><code>define("BASEDIR", __DIR__ . "/"); function __autoload($className){ $className = BASEDIR . "include/class/" . str_replace("_", "/", $className) . ".php"; echo $className . "&lt;br / &gt;"; require_once($className); } </code></pre>
    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.
    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