0

The goal

Get the name of a method that called another method.

The problem

I need to get a method name in another method, and now I'm using this:

$crazyDinosaur = new CrazyDinosaur(300, 600);
$crazyDinosaur->attack(new NinjaCat(), __FUNCTION__);

How can I discover that CrazyDinosaur attacked NinjaCat without the __FUNCTION__? Is it possible?

More details

What I exactly want is: x triggers an event. I don't want to say again that x triggered the event, because the event should know who's triggering itself.

Guilherme Oderdenge
  • 4,935
  • 6
  • 61
  • 96

3 Answers3

1

You would normally have an abstract Actor or Character class, sub-class them, and then create instances of the sub-classes. For example:

<?php
abstract class Actor {}

abstract class Animal extends Actor {}

class Dinosaur extends Animal {}

Classes, as you know, can have properties. So one property could be $type, which holds a string representation of the class. So if you had created an instance of Dinosaur, $type could simply be dinosaur.

So now we have:

<?php
abstract class Animal extends Actor
{
    protected $type;
}    

class Dinosaur extends Animal
{
    protected $type = 'dinosaur';

    public function getType()
    {
        return $this->type;
    }
}

class Cat extends Animal
{
    protected $type = 'cat';
}

$crazyDinosaur = new Dinosaur();
$crazyCat = new Cat();

I think your problem is you’ve attached the attack() method to the wrong class; I would attached it to the victim’s class. So if dinosaur is attacking cat, then I’d call the attack() method on the cat class, and pass your dinosaur object as a parameter. That way, you know what’s attacking what by just inspecting what type of class the $attacker is.

<?php
class Cat extends Animal
{
    public function attack(Actor $attacker)
    {
        // code here to do things like deduct cat's health etc.
    }
}

$crazyDinosaur = new Dinosaur();
$crazyCat = new Cat();

$crazyCat->attack($crazyDinosaur);

You can extend your attack method to accept additional parameters. For example, the strength of the attack to change the amount of damage it does to the victim.

I’d also suggest looking into the Observer pattern, as that’s ideal in this case. Also, PHP isn’t an event-driven language so isn’t the best fit for a game.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • Your sentence `$crazyCat->attack($crazyDinosaur);` sounds very strange. But I get your point of view. Hang on a second to try by myself. – Guilherme Oderdenge Nov 28 '13 at 17:57
  • *And this isn't a game, lol...* Actually, is an ErrorHandler. When an error happens, I need to know where it is happening. The context that I pass to the Stack Overflow is easier to understand and the scenario is *almost* the same. – Guilherme Oderdenge Nov 28 '13 at 17:59
  • You really shouldn’t change the context of questions, as otherwise I would have pointed you to the [debug_backtrace()](http://de3.php.net/debug-backtrace) function instead of writing a convoluted answer based on a game. – Martin Bean Nov 28 '13 at 21:03
0

how about this

class creature{

public $attack_strength;
public $health ;

public function attack($enemy){
  $damage = $this->attack_strength;
  $enemy->health -= $damage;
  log_event($this->name . " attacked " . $enemy->name . " making " . $demage . " points of damage");
}
}

class NinjaCat extends creature{

}

class crazyDinosaur extends creature{

}
Dima K
  • 130
  • 7
  • Your logic is good and we are almost there. But what I exactly want is: `x` triggers an event. I don't want to say again that `x` triggered the event, because the event should know who's triggering itself. – Guilherme Oderdenge Nov 28 '13 at 17:47
0

I think you are looking for get_class($this):

...
function attack($victim)
{
  $attacker = get_class($this);
  var_dump($attacker);
}
...
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Negative. I want to "pass" the class/method/function name just triggering an method. Example: `$say->hi();` and in `hi()`'s method I discover that `$say` is the trigger. – Guilherme Oderdenge Nov 28 '13 at 18:21
  • @GuilhermeOderdenge Something like this? http://stackoverflow.com/questions/19135959/get-variable-name-of-object-inside-the-object-php – jeroen Nov 28 '13 at 18:29