class_exists and autoload

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    class_exists and autoload

    A while ago I decided I like the idea of __autoload
    or what seems to be better spl_autoload_re gister(),
    so now all my projects use this.

    But a major problem arises using third party classes that make use of class_exists().
    Because class_exists will call autoload if the class has not been defined
    and autoload has.
    So unless your autoload function can map in the directory structure of the third party class a critical error occurs

    This is the most idiotic thing I have come across in PHP.
    Why should class_exists have the potential to crash a program?

    Has anybody found a suitable work-around for this bug that PHP DENY is a bug? http://bugs.php.net/bug.php?id=47001
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    My workaround is manually including files that are needed, and ignoring files that are not. It makes your code easier to follow. I, for one, am against the idea of the __autoload magic method.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      Code:
      class_exists("yourClass", false);
      _______________ ___
      reading the manual actually pays off.....

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Dormilich
        class_exists("y ourClass", false);
        reading the manual actually pays off.....
        I am aware of this Dormilich, but I don't use class_exists(), it is used in a third party class, JPGRAPH to be precise.
        These class_exists() calls are all over the place, and causing problems.

        kovik,
        I, for one, am against the idea of the __autoload magic method.
        it is actually spl_autoload_re gister() I use now, this allows the use of multiple 'autoload' functions and is reportedly less overhead.
        My workaround is manually including files that are needed, and ignoring files that are not. It makes your code easier to follow
        Well, that is debatable.
        I have some big projects where scripts may be called via a web-page or from a scheduled task or even from another project, using a combination of classes for MySql, MsSql, CSV/XLS, PDF, FTP......

        I had includes all over the place, in fact include_once was mandatory.
        spl_autoload_re gister() made the code easier to manage.
        I know the command 'new' will invoke a class without worrying where it is included.
        Much simpler if you are comfortable with classes.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          but I don't use class_exists(), it is used in a third party class, JPGRAPH to be precise.
          These class_exists() calls are all over the place, and causing problems.
          I admit, changing the class_exists() calls is tedious and bad style, though it would handle the problem, right?

          very tedious after looking at the website… did you mention that to the JpGraph developers?

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            did you mention that to the JpGraph developers?
            Its a thought. I have no doubt they will defend their reasoning.

            I don't like the idea of editing the classes but I like this idea instead of class_exists
            Code:
            if(in_array($class_name, get_declared_classes()));
            I may place additional code in my autoloader function that handles jpgraph classes

            Comment

            • dlite922
              Recognized Expert Top Contributor
              • Dec 2007
              • 1586

              #7
              If you have a good IDE, you can run a replace-all with regex for all files in the JpGraph directory.

              PHPED does this for me and shows me all the instances that it replace so I can quickly view any mistakes if any.

              It's quick, but dirty. You'd have to do this every time you upgrade the app, but then again how often is that really?




              Dan

              Comment

              Working...