英文:
Is there any way to use arrow function as a property of a class in php?
问题
I have created a class named calculator where I have set an arrow function as the property of the class. And I have passed that property to the init
method. Once I run this program, It is not generated output. My expected output is 15. The code is below.
我已经创建了一个名为calculator的类,在这个类中,我将一个箭头函数设置为类的属性,并将该属性传递给了init
方法。一旦运行这个程序,它没有生成输出。我期望的输出是15。以下是代码。
<?php
namespace Calculate;
class calculator{
public $sum = fn(...$args) => array_sum($args);
public function init(){
return $this->sum(5, 10);
}
}
$cal = new calculator();
print $cal->init(); // 期望的输出是15
But on the other hand, when I am putting the sum
variable outside of the class and using it from the init method, It is working then. And it's providing the output 15. The code is below.
但另一方面,当我将sum
变量放在类外部并在init
方法中使用它时,它就可以正常工作了。并且它提供了输出为15的结果。以下是代码。
$sum = fn(...$args) => array_sum($args);
class calculator{
public function init(){
global $sum;
return $sum(5, 10);
}
}
$cal = new calculator();
print $cal->init(); // 输出是15
So, my question is can not I declare the arrow function as property or method of a class? If we can, what is the process, please?
所以,我的问题是我不能将箭头函数声明为类的属性或方法吗?如果可以的话,请问是什么过程?
英文:
I have created a class named calculator where I have set an arrow function as the property of the class. And I have passed that property to the init
method. Once I run this program, It is not generated output. My expected output is 15. The code is below.
<?php
namespace Calculate;
class calculator{
public $sum = fn(...$args) => array_sum($args);
public function init(){
return $this->sum(5, 10);
}
}
$cal = new calculator();
print $cal->init(); // Expected Output is 15
But on the other hand, when I am putting the sum
variable outside of the class and using it from the init method, It is working then. And it's providing the output 15. The code is below.
$sum = fn(...$args) => array_sum($args);
class calculator{
public function init(){
global $sum;
return $sum(5, 10);
}
}
$cal = new calculator();
print $cal->init(); // Output is 15
So, my question is can not I declare the arrow function as property or method of a class? If we can, what is the process, please?
答案1
得分: 2
First, the arrow function should be assigned to a variable, not a class property. Arrow functions in PHP cannot be directly assigned as class properties. Instead, you can define a regular method that uses the arrow function internally. Here's an updated version of the code:
namespace Calculate;
class Calculator {
private $sum;
public function __construct() {
$this->sum = fn(...$args) => array_sum($args);
}
public function init() {
return ($this->sum)(5, 10);
}
}
$cal = new Calculator();
echo $cal->init(); // Output: 15
Now, when you run the code, it will correctly output 15, which is the expected result.
英文:
First, the arrow function should be assigned to a variable, not a class property. Arrow functions in PHP cannot be directly assigned as class properties. Instead, you can define a regular method that uses the arrow function internally. Here's an updated version of the code:
namespace Calculate;
class Calculator {
private $sum;
public function __construct() {
$this->sum = fn(...$args) => array_sum($args);
}
public function init() {
return ($this->sum)(5, 10);
}
}
$cal = new Calculator();
echo $cal->init(); // Output: 15
Now, when you run the code, it will correctly output 15, which is the expected result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论