简体   繁体   English

Laravel 集合 map 在同一个 class 中使用私有 function 而不是关闭

[英]Laravel collection map use private function in same class instead closure

I use the map function to loop each object of collections. The closure method is too long and I tried to make it a private function. But I cannot successfully call the function.我使用map function循环collections的每个object。关闭方法太长,我试图将其设为私有function。但我无法成功调用function。

Here is my sample code.这是我的示例代码。 (note: the function test may be very long in real case) (注意:function 测试在实际情况下可能会很长)

<?php

class cls
{
    private function test($a) {
        return ($a + 1);
    }

    public function run1() {
        return ($this->test(5));
    }

    public function run2() {
        $col = Collect([1,2,3]);
        return ($col->map($this->test()));
    }

    public function run3() {
        $col = Collect([1,2,3]);
        $mycls = $this;
        return ($col->map(function ($c) use ($mycls) {
            return($mycls->test($c));
        }));
    }
}

$c = new cls;
$c->run1(); # 6
$c->run2(); # Error: Too Few Argements
$c->run3(); # [2,3,4]

I use the function: run1 to test if the private function is callable and I failed at function: run2.我使用 function: run1 来测试私有 function 是否可调用,但我在 function: run2 失败了。 Although function: run3 makes code shorten.虽然 function: run3 使代码变短了。 It seems a little bit too superfluous.似乎有些多余。 How can I make run2 works?我怎样才能使 run2 工作?

Update更新

My version of Laravel is 6.2我的Laravel版本是6.2

Update更新

I tried the @xenooooo answer.我尝试了@xenooooo 的回答。 It works with the public function, but I get a different error code with the private method.它适用于公共 function,但我使用私有方法得到了不同的错误代码。 在此处输入图像描述

Simple answer: $this->test() required a parameter and you are simply not passing anything to it.简单的回答: $this->test()需要一个参数,而你只是没有传递任何东西给它。

You can also modify your run2/run3 methods to do the following:您还可以修改run2/run3方法以执行以下操作:

public function run2() {
   $col = collect([1,2,3])->map(function ($value) {
      return $this->test($value);
   });
        
   return $col; //returns a collection of items
   //return $col->toArray(); //(extra option) returns an array of collected items
}

Result:结果:

Illuminate\Support\Collection {#872 ▼ 
  #items: array:3 [▼
    0 => 2
    1 => 3
    2 => 4
  ]
  #escapeWhenCastingToString: false
}

You may read more on collections here: https://laravel.com/docs/9.x/collections#introduction您可以在此处阅读有关 collections 的更多信息: https://laravel.com/docs/9.x/collections#introduction

The problem in run2 is that you are calling the method directly and you need to pass the an argument since test is expecting argument $a . run2中的问题是您直接调用该方法并且需要传递 an 参数,因为test需要参数$a To use the method as a callback function you need the pass it as an array which is the first value is $this and the second is the method name which is test :要将该方法用作回调 function,您需要将其作为数组传递,第一个值是$this ,第二个是方法名称test

public function test($a) {
    return ($a + 1);
}

public function run2()
{
    $collection = collect([1,2,3]);

    $newCollection = $collection->map([$this,'test']);

    return $newCollection;
}

UPDATE更新

It only work if the method that you will call is public and it will not work if you use a private or protected method.它仅在您将调用的方法是公共方法时才有效,如果您使用私有或受保护方法则它将无效。 If you use a private or protected if will throw a BadMethodCallException如果你使用 private 或 protected if 将抛出BadMethodCallException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM