SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 95
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    __autoload is overrated!

    I think using autoload is a more of a pain in the neck than just using require_once method.

    1. The original coding stardard used for naming classes used to be like this:

    ControllerUserGroup and now because of zend people have changed to Controller_User_Group.

    You should not forced to rename your classes. A better way of autoload classes should have been thought up before releasing autoload.


    2. You can not have gaps in your filenames.

    For example:

    controller/user_group.php

    Autoload will try to look for controller/user/group.php

    Even Ruby-On-Rails uses a require method.

    Does anyone else here prefer to use require_once instead of __autoload?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I use autoload, much cleaner source files, and you only need to include files you really need.

    You do not need to use class_name you can continue to use ClassName if you like I even made a method to translate camel case to separators.

    PHP Code:
    $class joinpreg_split'/([A-Z][^A-Z]+)/'$class, -1PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE ), '/' ); 
    For Number two that is harder to do dynamically, but in a production environment you should use an index anyways, its a lot less load on the server then dynamically trying to find the correct class source file. I already posted an example of an index based autoloader. http://www.sitepoint.com/forums/show...5&postcount=19
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    I think using autoload is a more of a pain in the neck than just using require_once method.

    1. The original coding stardard used for naming classes used to be like this:

    ControllerUserGroup and now because of zend people have changed to Controller_User_Group.

    You should not forced to rename your classes. A better way of autoload classes should have been thought up before releasing autoload.


    2. You can not have gaps in your filenames.

    For example:

    controller/user_group.php

    Autoload will try to look for controller/user/group.php

    Even Ruby-On-Rails uses a require method.

    Does anyone else here prefer to use require_once instead of __autoload?
    You can implement __autoload in any way you like... you don't have to follow zend conventions.

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    I think using autoload is a more of a pain in the neck than just using require_once method.

    1. The original coding stardard used for naming classes used to be like this:

    ControllerUserGroup and now because of zend people have changed to Controller_User_Group.

    You should not forced to rename your classes. A better way of autoload classes should have been thought up before releasing autoload.


    2. You can not have gaps in your filenames.

    For example:

    controller/user_group.php

    Autoload will try to look for controller/user/group.php

    Even Ruby-On-Rails uses a require method.

    Does anyone else here prefer to use require_once instead of __autoload?
    Are you talking about a custom __autoload function you wrote? I don't see why there would be a problem...

    PHP Code:
    function __autoload($class)
    {
        
    $pathArray explode(':'$include_path);
        foreach(
    $pathArray as $path)
        {
            
    $file $path '/' $class;
            if(
    file_exists($file))
            {
                require_once 
    $file;
                return;
            }
        }
        throw new 
    Exception($class ' not found');

    ...
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    1. The original coding stardard used for naming classes used to be like this:

    ControllerUserGroup and now because of zend people have changed to Controller_User_Group.
    The PEAR code standard has been around for a decade or such. Zend Framework just adopted the already established convention.

  6. #6
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    The PEAR code standard has been around for a decade or such. Zend Framework just adopted the already established convention.
    Wait... is he complaining about ZF? Because ZF has a pretty clear coding standard.

    I was taught to adhere to the standards of the person who pays and not necessarily my own. My preferred style is the one that pays my bills on time :3
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  7. #7
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    PHP Code:
    function __autoload($class) {
        throw new 
    Exception($class ' not found');

    I'd like to note that throwing an exception inside autoload is a fatal error. The function should return a boolean value indicating whether the class was found or not.

  8. #8
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ezku View Post
    I'd like to note that throwing an exception inside autoload is a fatal error. The function should return a boolean value indicating whether the class was found or not.
    OO really? That's good to know :3. That's what I get for not RTFM-ing correctly >.>...
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  9. #9
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For those people that use autoload, do you use it for every class?

    Do you use it for all classes within the controllers, models, helpers, libraries etc..

  10. #10
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    For those people that use autoload, do you use it for every class?

    Do you use it for all classes within the controllers, models, helpers, libraries etc..
    yes i do. my overwritten __autoload function uses regex to pick out whether it's a controller, model, helper, or third-party plugin. with this information, it picks out which directory to load it from (/models or /controllers or /helpers or /plugins).

    also ControllerUserGroup sounds really weird...i prefer UserGroupController, but maybe that's just me.

  11. #11
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yah, I use __autoload for just about everything when I developing an application. If its a small application though, its pointless to use __autoload so in those cases, I won't bother. The way I see it it's my time vs. server's time... Why work for my code when I can have my code work for me :3
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  12. #12
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very good so far.

    My next question then is how to structure your folders and files so autoload works correctly.

    Example:

    library
    helper
    database

    application
    +controller
    +model
    +view

    include_path = .:/library.:/helper.:/database:./application

  13. #13
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    2. You can not have gaps in your filenames.

    For example:

    controller/user_group.php

    Autoload will try to look for controller/user/group.php
    That's strange, if true. I've never had any trouble with __autoload arbitrarily converting underscores to slashes. Or do I completely misunderstand this statement? As the previous poster said, it matches whatever you tell it to match.

  14. #14
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    My next question then is how to structure your folders and files so autoload works correctly.
    If you use multiple naming conventions or store your class files in multiple places, you will need to write your __autoload function to try every convention and/or path. It's up to you how complicated you want to make it.

  15. #15
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    My next question then is how to structure your folders and files so autoload works correctly.
    well to be honest i wouldn't use __autoload() if you couldn't override it. i configure the logic in __autoload to find my files in my preferred directory structure...i don't rearrange my directory structure to accomodate for __autoload.

  16. #16
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    If you use multiple naming conventions or store your class files in multiple places, you will need to write your __autoload function to try every convention and/or path. It's up to you how complicated you want to make it.
    Or write a new function for each convention and use spl_autoload_register().

  17. #17
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allspiritseve View Post
    Or write a new function for each convention and use spl_autoload_register().
    Either way, the bottom line is still this: It's down to the programmer to make __autoload work correctly for his application. Any issues with making it work are not the fault of __autoload itself. It merely provides the "magical" functionality to the user's own function.

  18. #18
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what file structure are people using?

  19. #19
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is how i prefer to do it...

    Code:
    /app
    -/models
    -/views
    -/controllers
    /config
    /doc
    /logs
    /plugins
    -/recaptcha
    -/doctrine
    -/wiki
    /test
    -/models
    -/controllers
    i have seen many variations on this...i know it's very similar to how ruby on rails does it...and I think Zend Framework slightly resembles this way as well

  20. #20
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since I use Zend Framework at work, adhere to Zend Framework's Conventional Modular Directory Structure.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  21. #21
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    I think using autoload is a more of a pain in the neck than just using require_once method.
    ...
    Does anyone else here prefer to use require_once instead of __autoload?
    Most certainly. Autoload seems to assume one class per file. That's not a good way to organise code.

    Also naming is far too important and difficult to be mixed up with the file system.

    I think autoload hints at a bigger problem: object creation. What you really want to look at is dependency injection.


  22. #22
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff View Post
    Most certainly. Autoload seems to assume one class per file. That's not a good way to organise code.
    I disagree, but I'm intrigued - why do you prefer to keep multiple classes in one file?

    Personally I prefer to keep one class per file, because it makes maintenance smoother, e.g. If I want to check some code in the Article class, I go to Classes/Article.php
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  23. #23
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    I disagree, but I'm intrigued - why do you prefer to keep multiple classes in one file?

    Personally I prefer to keep one class per file, because it makes maintenance smoother, e.g. If I want to check some code in the Article class, I go to Classes/Article.php
    I do it that way too, but there are some cases where I would consider putting several classes in the same file. For example, if some of the classes are helper classes that are only ever used in conjunction with the main class in the file. In that case it wouldn't matter if __autoload couldn't find each class because they would already be declared when the main class is declared.

  24. #24
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using a function called register() which is used by the class definitions to register a library (as a directory) to the include_path directive. That way __autoload doesn't have to reconstruct the include paths each time.

  25. #25
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One thing that annoyes me about zend is that they created their own coding standard rather tham use a current one like GNU.


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
  •