php 我可以在另一个函数中包含一个函数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/708341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I include a function inside of another function?
提问by chaos
Is it possible to include one function inside another? To learn functions, I'm trying to create a combat sequence using PHP. The sequence would look like this:
是否可以将一个函数包含在另一个函数中?为了学习函数,我正在尝试使用 PHP 创建一个战斗序列。该序列将如下所示:
- Dice would roll, assigning numbers to variables;
- Hero1 attack results are printed;
- Dice would roll, assigning numbers to variables;
- Hereo2 attack results are printed;
- Dice would roll, assigning numbers to variables;
- Hero3 attack results are printed.
- 骰子会滚动,为变量分配数字;
- 打印 Hero1 攻击结果;
- 骰子会滚动,为变量分配数字;
- Hereo2攻击结果被打印出来;
- 骰子会滚动,为变量分配数字;
- 打印 Hero3 攻击结果。
The dice rolling would be an automated function. Here is the code:
掷骰子将是一项自动化功能。这是代码:
<?
rollSim();
combatSim();
function rollSim() {
$hAttack = rand(1,20);
$mDefend = rand(1,20);
$mAttack = rand(1,20);
$hDefend = rand(1,20);
$mDamage = rand(10,25);
$hDamage = rand(1,20);
} // end rollSim
function combatSim() {
rollSim();
if ($hAttack>$mDefend) {
print "Hero hit monster for $hDamage damage.<br>";
} else if ($hAttack<=$mDefend) {
print "Hero missed monster.";
}
} // end combatSim
?>
回答by Jeremy Ruten
Your rollSim()function should returnthe rolled numbers rather than setting some variables and trying to use them in your other function. I would return them in an associative array, like this:
您的rollSim()函数应该返回滚动的数字,而不是设置一些变量并尝试在其他函数中使用它们。我会在关联数组中返回它们,如下所示:
function rollSim() {
$roll['hAttack'] = rand(1,20);
$roll['mDefend'] = rand(1,20);
$roll['mAttack'] = rand(1,20);
$roll['hDefend'] = rand(1,20);
$roll['mDamage'] = rand(10,25);
$roll['hDamage'] = rand(1,20);
return $roll;
}
function combatSim() {
$roll = rollSim();
if ($roll['hAttack'] > $roll['mDefend']) {
print "Hero hit monster for {$roll['hDamage']} damage.<br>";
} else if ($roll['hAttack'] <= $roll['mDefend']) {
print "Hero missed monster.";
}
}
回答by chaos
No, you can't really do what you're asking. Even if you embedded the declaration of rollSim()inside the definition of combatSim()(which you can do, that's legal but has no real effects), the variables you're setting in rollSim()would still be local to it and inaccessible by combatSim().
不,你不能真正做到你所要求的。即使您将 of 的声明嵌入在 ofrollSim()的定义中combatSim()(您可以这样做,这是合法的但没有实际效果),您设置的变量rollSim()仍然是它的本地变量,并且无法通过combatSim().
You need a better way of passing around the information you're concerned with. Jeremy Ruten details a good way. Another way would be to define an object that's responsible for modeling your combat event and have rollSim()and combatSim()both be methods on it.
您需要一种更好的方式来传递您关心的信息。Jeremy Ruten 详细介绍了一个好方法。另一种方法是定义一个对象,该对象负责对您的战斗事件进行建模,rollSim()并且combatSim()在其上具有和都是方法。
class myCombat {
private $hAttack;
private $mDefend;
private $mAttack;
private $hDefend;
private $mDamage;
private $hDamage;
function rollSim() {
$this->hAttack = rand(1, 20);
$this->mDefend = rand(1, 20);
$this->mAttack = rand(1, 20);
$this->hDefend = rand(1, 20);
$this->mDamage = rand(10, 25);
$this->hDamage = rand(1, 20);
}
function combatSim() {
$this->rollSim();
if($this->hAttack > $this->mDefend)
echo 'Hero hit monster for ' . $this->hDamage . ' damage.<br />';
else
echo 'Hero missed monster';
}
}
$combat = new myCombat;
$combat->combatSim();
回答by Kalium
You can call functions from one another, certainly... but I think you want to use the scope of one in another, correct?
您可以从另一个调用函数,当然……但我认为您想在另一个中使用一个的范围,对吗?
Sharing scope is a messy business. You're better off passing arguments.
共享范围是一项混乱的业务。你最好传递参数。
回答by zokibtmkd
If you want to define functions within function in PHP you can do it like this:
如果你想在 PHP 中的函数内定义函数,你可以这样做:
function a()
{
function b()
{
echo 'I am b';
}
function c()
{
echo 'I am c';
}
}
a();
b();
c();
You must call the parent function first, then the functions inside.
你必须先调用父函数,然后调用里面的函数。
回答by Mang Jojot
here is the simple example call function within function
这是函数内的简单示例调用函数
class Percobaan{
public function coba(){
return "return value";
}
public function call(){
//call coba function
return $this->coba();
}
} // end of class percobaan
//call the method
$klass = new Percobaan();
echo $klass->call();
the output willbe :
输出将是:
"return value"
回答by Alan
That would be a closure if you would.
如果你愿意,那将是一个关闭。

