我正在尝试利用 PHP 中的自动加载。我在不同的目录中有各种类,所以我引导自动加载如下:
function autoload_services($class_name)
{
$file = 'services/' . $class_name. '.php';
if (file_exists($file))
{
require_once($file);
}
}
function autoload_vos($class_name)
{
$file = 'vos/' . $class_name. '.php';
if (file_exists($file))
{
require_once($file);
}
}
function autoload_printers($class_name)
{
$file = 'printers' . $class_name. '.php';
if (file_exists($file))
{
require_once($file);
}
}
spl_autoload_register('autoload_services');
spl_autoload_register('autoload_vos');
spl_autoload_register('autoload_printers');
这一切似乎都很好,但我只是想仔细检查一下这确实被认为是可以接受的做法。
最佳答案
当然,看起来不错。您唯一可以做的就是按照它们最有可能命中的顺序注册它们。例如,如果您最常用的类是服务,然后是 vos,然后是打印机,那么您拥有的顺序是完美的。这是因为它们是按顺序排队和调用的,所以这样做会稍微提高性能。
关于PHP spl_autoload_register,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3710480/
在我的代码中,我使用自动加载进行惰性评估,这样我可以更快地加载程序并在需要时加载文件,我没有看到很多人使用它,但在Thin项目中我注意到自动加载已被广泛使用,反正只是想知道使用它是否有任何风险。 最佳答案 autoload是notthreadsafe并将在未来的Ruby版本中弃用。这是proofbyMatz(ruby的创造者)。 关于ruby-使用autoload与ruby中的require进行惰性评估?,我们在StackOverflow上找到一个类似的问题:
我有这门课:#app/events/new_request.rbclassEvents::NewRequestend然后我将该文件夹添加到自动加载:#config/application.rbconfig.autoload_paths+=%W(events/)当运行railsc时:>Events::NewRequestNameError:uninitializedconstantEvents问题是,如果我在定义类时不使用命名空间“Events”,它会成功自动加载类。 最佳答案 moduleSandboxclassApplicatio
我在为包含在模型中的模块命名空间时遇到了一些麻烦。在/app/models/car.rb中classCarincludeSearch::Carend在/lib/search/car.rb中moduleSearchmoduleCarincludeActiveSupport::Concern#methodsinhereendend在/config/application.rb中config.autoload_paths+=Dir["#{config.root}/lib/**/"]config.autoload_paths+=Dir["#{config.root}/lib/search/*"
我通过引入请求和响应模型来重构我的Controller,以执行此presentation之后Controller周围的一些逻辑。.我分别用模块Responses和Requests包装了所有的响应和请求模型。应用程序运行完美,但当我运行测试时,出现以下错误。Failure/Error:UnabletofindmatchinglinefrombacktraceRuntimeError:CirculardependencydetectedwhileautoloadingconstantResponses::FolderContentResponse我的目录结构如下:-应用/-模型/-回应/注
我正在尝试使用FactoryGirl运行rspec,但我一直收到此错误:1)ProductsUpdatewithinvalidinformationFailure/Error:let(:product){FactoryGirl.create(:product)}ArgumentError:Factorynotregistered:product#./spec/requests/products_spec.rb:47:in`block(3levels)in'#./spec/requests/products_spec.rb:52:in`block(3levels)in'-这是有错误的测试
如果我遗漏了什么,请告诉我。我不明白为什么无法访问我的views/references/文件夹。new.html.erb和index.html.erb都不可用。当我转到localhost:3000/references时,我的错误是:RuntimeErrorinReferencesController#indexCirculardependencydetectedwhileautoloadingconstantReferencesController我相信这是设置,它不应该是Rails问题,因为我的其他Controller工作正常。我的路线文件中有resources:reference
跟随BenWalker的(惊人的)Let'sBuildInstagramWithRails,特别是BDD版本。教程使用FactoryGirl。我在多次测试中遇到以下错误:精简版Failure/Error:post=create(:post,user_id=user.id)ArgumentError:Traitnotregistered:1我什至无法让Ben用cloneofmyrepo重新创建错误,我在StackOverflow的“特征未注册”问题中找不到任何内容。这是我的第一个SO问题,所以如果我在那个前面做错了什么,请告诉我。在此先感谢您的帮助!代码选择:spec/factories
我正在查看active_support.rb以尝试了解它使用的加载过程。它使用三种加载方法:load_all!、autoload和require。为什么在同一个文件中使用三种不同的加载方式?moduleActiveSupportdefself.load_all![Dependencies,Deprecation,Gzip,MessageVerifier,Multibyte,SecureRandom,TimeWithZone]endautoload:BacktraceCleaner,'active_support/backtrace_cleaner'autoload:Base64,'ac
我有一个Rails应用程序,其中有一个Rake任务,该任务使用并发rubygem提供的多线程函数。有时我会遇到Circulardependencydetectedwhileautoloadingconstant错误。在谷歌搜索了一下后,我发现这与结合使用线程和加载Rails常量有关。我偶然发现了以下GitHub问题:https://github.com/ruby-concurrency/concurrent-ruby/issues/585和https://github.com/rails/rails/issues/26847如此处所述,您需要将从新线程调用的所有代码包装在Rails.a
几周前读到autoload是officiallydeprecated,Matz不鼓励使用它。用什么来代替它?开发人员应该怎么做?我在一些命令行gem中使用它来避免不必要地加载可能永远不会使用的库,并在JRuby中使用它来防止对.jar文件进行相同的加载。 最佳答案 我见过人们使用EasyLoadgem,它声称是一个autoload替代品。它根据目录模块命名约定加载。 关于ruby-随着'autoload'被弃用,开发者应该同时使用什么?,我们在StackOverflow上找到一个类似的问