SitePoint Sponsor |
|
User Tag List
Results 1 to 25 of 95
Thread: __autoload is overrated!
-
Nov 20, 2008, 19:58 #1
- 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?
-
Nov 20, 2008, 20:30 #2
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 = join( preg_split( '/([A-Z][^A-Z]+)/', $class, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ), '/' );
-
Nov 20, 2008, 20:32 #3
- Join Date
- Dec 2002
- Location
- Ann Arbor, MI (USA)
- Posts
- 648
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 21, 2008, 00:41 #4
- Join Date
- Jan 2005
- Location
- heaven
- Posts
- 953
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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!
-
Nov 21, 2008, 03:19 #5
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 21, 2008, 03:39 #6
- Join Date
- Jan 2005
- Location
- heaven
- Posts
- 953
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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 :3Creativity knows no other restraint than the
confines of a small mind. - Me
Geekly Humor
Oh baby! Check out the design patterns on that framework!
-
Nov 21, 2008, 06:23 #7
- Join Date
- May 2005
- Location
- Finland
- Posts
- 608
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 21, 2008, 06:40 #8
- Join Date
- Jan 2005
- Location
- heaven
- Posts
- 953
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 21, 2008, 08:30 #9
- 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..
-
Nov 21, 2008, 09:11 #10
- Join Date
- Nov 2004
- Location
- Plano
- Posts
- 643
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Nov 21, 2008, 09:51 #11
- 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!
-
Nov 21, 2008, 12:00 #12
- 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
-
Nov 21, 2008, 12:01 #13
- Join Date
- Apr 2007
- Posts
- 1,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 21, 2008, 12:07 #14
- Join Date
- Apr 2007
- Posts
- 1,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 21, 2008, 12:08 #15
- Join Date
- Nov 2004
- Location
- Plano
- Posts
- 643
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 21, 2008, 12:43 #16
- Join Date
- Dec 2002
- Location
- Ann Arbor, MI (USA)
- Posts
- 648
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 21, 2008, 13:13 #17
- Join Date
- Apr 2007
- Posts
- 1,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Nov 21, 2008, 16:47 #18
- Join Date
- Feb 2006
- Posts
- 281
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
what file structure are people using?
-
Nov 21, 2008, 16:55 #19
- 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
-
Nov 21, 2008, 19:37 #20
- 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!
-
Nov 22, 2008, 09:06 #21
- Join Date
- Sep 2003
- Location
- Glasgow
- Posts
- 1,690
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
working on: Aperiplus, Rephactor, Phemto
useful links: xUnit test patterns, martinfowler.com, c2 wiki
-
Nov 22, 2008, 09:29 #22
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Nov 22, 2008, 12:23 #23
- Join Date
- Apr 2007
- Posts
- 1,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Nov 23, 2008, 02:30 #24
- 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.
-
Nov 23, 2008, 11:39 #25
- 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