Zend Framework

Add interface awareness to Zend::loadClass()

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Trivial Trivial
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 0.9.0
  • Component/s: Zend_Loader
  • Labels:
    None

Description

Zend::loadClass() uses class_exists() as a test to see if the class has been previously loaded and/or determine if the class was successfully loaded following a loadFile() operation. This poses a problem as an interface will cause class_exists() to report false, and thus using Zend::loadClass() in autoload will often result in false negatives.

A simple solution is presented in the following diff:

Index: Zend.php
===================================================================
--- Zend.php    (revision 3503)
+++ Zend.php    (working copy)
@@ -68,7 +68,7 @@
      */
     static public function loadClass($class, $dirs = null)
     {
-        if (class_exists($class, false)) {
+        if (class_exists($class, false) || interface_exists($class, false)) {
             return;
         }
 
@@ -102,7 +102,7 @@
 
         self::loadFile($file, $dirs, true);
 
-        if (!class_exists($class, false)) {
+        if (!class_exists($class, false) && !interface_exists($class, false)) {
             throw new Zend_Exception("File \"$file\" was loaded "
                                . "but class \"$class\" was not found within.");
         }

Activity

Hide
Gavin added a comment -

This change is nicely unobtrusive , and won't lead to the confusion that existed when we had both loadClass() and loadInterface(). However, the burden is on us to show clear use cases justifying supporting the loading of interfaces this way.

Show
Gavin added a comment - This change is nicely unobtrusive , and won't lead to the confusion that existed when we had both loadClass() and loadInterface(). However, the burden is on us to show clear use cases justifying supporting the loading of interfaces this way.
Hide
Shaun Rowe added a comment -

I would also like to see this implemented. This presented itself to me as a problem just last week and I was going to suggest a very similar fix, I also agree with Gavin's comment.

Show
Shaun Rowe added a comment - I would also like to see this implemented. This presented itself to me as a problem just last week and I was going to suggest a very similar fix, I also agree with Gavin's comment.
Hide
Matthew Weier O'Phinney added a comment -

Additions complete with revision 3655

Show
Matthew Weier O'Phinney added a comment - Additions complete with revision 3655

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: