jjzjj

php - Laravel 助手接口(interface)

coder 2024-05-02 原文

简单的例子,我有以下用于计算的辅助接口(interface):

interface Calculation
{
    public function calculate();
}

还有几个实现这个接口(interface)的类:

class One implements Calculation
{
    public function calculate()
    {
        return some calculation;
    }
}


class Two implements Calculation
{
    public function calculate()
    {
        return some different calculation;
    }
}

// and so on..

现在,在我看来,我有下面显示的代码,但我希望能够摆脱此处的 if。我希望能够做类似的事情:

foreach($units as $unit)
{
        $total = (new \App\Helpers\{$unit})->calculate();
}

我该怎么做?

foreach($units as $unit)
{
    if($unit === 'One')
    {
        $total = (new \App\Helpers\One)->calculate();
    }

    if($unit === 'Two')
    {
        $total = (new \App\Helpers\Two)->calculate();
    }

    if($unit === 'Three')
    {
        $total = (new \App\Helpers\Three)->calculate();
    }

    if($unit === 'Four')
    {
        $total = (new \App\Helpers\Four)->calculate();
    }

    // and so on..
}

最佳答案

试试这段代码:

   foreach($units as $unit)
    {
     $class="\\App\\Helpers\\".$unit;
     $total=(new $class)->calculate();
    }

关于php - Laravel 助手接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35253161/

有关php - Laravel 助手接口(interface)的更多相关文章

  1. postman接口测试工具-基础使用教程 - 2

    1.postman介绍Postman一款非常流行的API调试工具。其实,开发人员用的更多。因为测试人员做接口测试会有更多选择,例如Jmeter、soapUI等。不过,对于开发过程中去调试接口,Postman确实足够的简单方便,而且功能强大。2.下载安装官网地址:https://www.postman.com/下载完成后双击安装吧,安装过程极其简单,无需任何操作3.使用教程这里以百度为例,工具使用简单,填写URL地址即可发送请求,在下方查看响应结果和响应状态码常用方法都有支持请求方法:getpostputdeleteGet、Post、Put与Delete的作用get:请求方法一般是用于数据查询,

  2. ruby - Hanami link_to 助手只呈现最后一个元素 - 2

    我是HanamiWorld的新人。我已经写了这段代码:moduleWeb::Views::HomeclassIndexincludeWeb::ViewincludeHanami::Helpers::HtmlHelperdeftitlehtml.headerdoh1'Testsearchengine',id:'title'hrdiv(id:'test')dolink_to('Home',"/",class:'mnu_orizontal')link_to('About',"/",class:'mnu_orizontal')endendendendend我在模板上调用了title方法。htm

  3. ruby-on-rails - 将 Rails 路由助手作为类方法添加到类中 - 2

    我如何将像“root_path”这样的Rails路由助手作为类方法添加到像my_model.rb这样的类中?所以我的课是这样的:ClassMyModeldefself.fooreturnself.root_pathendendMyModel.foo以上不起作用,因为ClassMyModel不响应root_path这是我所知道的:我可以使用includeRails.application.routes.url_helpers,但这只会将模块的方法添加为实例方法我试过扩展Rails.application.routes.url_helpers但它没用请随时给我上课:)

  4. ruby 私有(private)类方法助手 - 2

    您好,我正在尝试创建一个帮助程序,用于将ruby​​方法大量定义为私有(private)类方法。通常,可以通过使用private_class_method键工作将方法定义为私有(private)类方法。但我想创建一个以下样式的助手:classPersondefine_private_class_methodsdodefmethod_oneenddefmethod_twoendendend我计划通过以下方式动态定义它,但根本不起作用:classObjectdefself.define_private_class_methods&blockinstance_evaldoprivate&bl

  5. ruby-on-rails - RSpec 应用程序助手测试 : Undefined local variable or method `helper` - 2

    有一段时间这似乎是一个闪烁的错误,但现在它一直出现:当我在一个相当简单的ApplicationHelper规范上运行RSpec时,我得到以下错误:%rspec--backtrace1)ApplicationHelperrendersMarkdownfromplaintextFailure/Error:expect(helper.md(plaintext)).toeq("Header\n")NameError:undefinedlocalvariableormethod`helper'for##/Users/danielsh/.rvm/gems/ruby-2.1.1@project-st

  6. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  7. jquery - 在 Rails 中从原型(prototype)切换到 jquery,助手呢? - 2

    我目前从prototype切换到jquery主要是为了支持简单的ajax文件上传。我使用:https://github.com/indirect/jquery-rails95%的javascript代码是由railshelper编写的,例如:-remote_function-render:updatedo|page|-page.replace_html'id',:partial=>'content'-page['form']['name']=something-page.visual_effect:highlight,'head_success'...我知道我必须为Jquery重写5%

  8. ruby-on-rails - 如何在 RubyOnRails 中使用 'acts as nested set' 创建一个可排序的接口(interface) - 2

    我一直在为使用acts_as_list的模型实现一些不错的交互界面,这些界面可以对我的mRails应用程序中的列表进行排序。我有一个排序函数,在每次拖放之后使用sortable_elementscript.aculo.us函数调用并设置每条记录的位置。这是在拖放完成后处理排序的Controller操作示例:defsortparams[:documents].each_with_indexdo|id,index|Document.update_all(['position=?',index+1],['id=?',id])endend现在我正在尝试对嵌套集模型(acts_as_nested

  9. ruby - Sinatra 助手伪造请求 - 2

    总结在Sinatra内Web应用程序,如何向应用程序发出虚拟请求并以文本形式返回响应正文?例如,这些路线...get('/foo'){"foo"}get('/bar'){"#{spoof_request'/foo'}-bar"}...当使用Web浏览器请求“/bar”时,应产生响应“foo-bar”。动机我的应用程序有一个代表错误条目的页面,其中包含关于该错误条目的大量详细信息:错误出现在哪个版本中,它有多重要,与之关联的标签是什么,错误分配给了谁等等.用户可以在此页面上以交互方式编辑单个数据。使用我的AJAXFetchjQuery插件,JavaScript使用AJAX将页面的只读部分

  10. ruby-on-rails - "uninitialized constant"当包含测试助手模块时 - 2

    我在尝试将辅助模块包含到测试中时遇到未初始化的常量错误。我的rails测试目录中有以下文件functional>admin>school_controller_test.rbfunctional>controller_helper.rb类/模块主体如下:moduleControllerHelperdefcheck_sort_order(items,column,direction)...endendclassAdmin::SchoolsControllerTest当我运行它时,测试输出是:/.../.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.

随机推荐