SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with superclass including

    Alright, here is my deal: I have a directory that contains other directories (ok, don't laugh yet.)

    I have a function that will include class files from inside of the internal directories. The function sorts the files in the directory alphabetically, and then includes them one by one.

    This works great, but if a child class comes alphabetically before a superclass, ex// 'DirectoryIterator', and 'Iterator' (which is coincedentaly one of my problems ), then as soon as DirectoryIterator.php is included, it complains that it can't find superclass Iterator.

    Does anyone know a way that I can just shut PHP up until all of the include statements have been run? That seems a little hacky, so maybe there is some way to do it that at least in theory sounds better?

    If anyone has run into this problem before, you probably have a good idea of what i'm talking about.

    Just ask for further clarification if you need it. Thanks a million, guys (and gals, i assume )

    Gybbyl
    Ryan

  2. #2
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just out of curiousity, can you tell me why you want to sort them before including them?

  3. #3
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I don't, but the way the directory is set up (I'm using Windows XP) It's ordering them alphabetically. I'm not explicity doing it. It will be ordered different on any machine -- date, name, size, inverse of those... It's impossible to tell.

    So maybe I just worded that wrong.

    Quote Originally Posted by Gybbyl
    The function sorts the files in the directory alphabetically, and then includes them one by one.
    Quickly hacking an edited draft

    The function reads all of the files in the directory based on how the OS is sorting them, and then includes them one by one.
    Ryan

  4. #4
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just include superclasses in the subclass definition file itself. Use include_once() all around and you won't step on your own toes.

  5. #5
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, that's probably the best way to do it.

    I'm still not QUITE clear on include_once()... Say if I have two subclasses including the same class, will it complain that I am overriding the superclass???
    Ryan

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you not reverse the sort ie z-a instead of a-z ?

    Another way around it may be that you put the super classes in their own folder and include these first, before the child classes - if it's an option ?

  7. #7
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I can't count on them being ordered in any specific way, so that's kinda out, unless I read all the names and then order them that way, but that won't work either.

    And, I'm grouping the classes by funtion, like

    Code:
    /iterator
    /iterator/Iterator.php
    /iterator/QueryIterator.php
    ...
    Query iterator relies on the iterator interface, so I want to keep them in the same directory. I think I'm just going to end up including the superclasses in the child class, even though I hate the thought of hardcoding an include.

    Oh well, thanks anyway. :-)
    Ryan

  8. #8
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about naming your classes like this:

    Code:
    /iterator/Iterator.php
    /iterator/IteratorQuery.php
    /iterator/IteratorQueryReverse.php
    i.e. classSubclassSubclass2Subclass3Etc.php. When this is sorted alphabetically, the parent class will always be included before the subclass.

    But it's not a nice solution. I think that a class file should always files for the other classes/interfaces it needs / extends / implements.

    Does it really hurt that much to put a statement like
    PHP Code:
    require_once 'iterator/Iterator.php'
    at the top of each file?

  9. #9
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I guess it doesn't hurt anything... I just wish there was a more saavy, 'java'-like way to do it. That would be sufficiently 'slick.'

    Maybe someday, eh?
    Ryan


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •