Can I include a function inside of another function?

后端 未结 6 1130
遥遥无期
遥遥无期 2021-01-02 06:28

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:

相关标签:
6条回答
  • 2021-01-02 06:34

    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.

    0 讨论(0)
  • 2021-01-02 06:43

    If you want to define functions within function in PHP you can do it like this:

    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.

    0 讨论(0)
  • 2021-01-02 06:45

    That would be a closure if you would.

    0 讨论(0)
  • 2021-01-02 06:51

    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().

    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.

    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();
    
    0 讨论(0)
  • 2021-01-02 06:53

    Your rollSim() function should return the 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:

    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.";
        }
    }
    
    0 讨论(0)
  • 2021-01-02 06:58

    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"
    
    0 讨论(0)
提交回复
热议问题