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.

3
  • $crazyDinosaur->attack(new NinjaCat(), $crazyDinosaur); ??? Commented Nov 28, 2013 at 17:34
  • In general your code is the easiest way. See stackoverflow.com/questions/2110732/… it might help you. Commented Nov 28, 2013 at 17:35
  • Isn't there a way to discover whose attacked the NinjaCat inside of the attack's method? Commented Nov 28, 2013 at 17:41

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Your sentence $crazyCat->attack($crazyDinosaur); sounds very strange. But I get your point of view. Hang on a second to try by myself.
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.
You really shouldn’t change the context of questions, as otherwise I would have pointed you to the debug_backtrace() function instead of writing a convoluted answer based on a game.
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{ } 

1 Comment

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.
0

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

... function attack($victim) { $attacker = get_class($this); var_dump($attacker); } ... 

2 Comments

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.
@GuilhermeOderdenge Something like this? stackoverflow.com/questions/19135959/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.