0

Possible Duplicate:
How to tell whether I’m static or an object?
How can I tell if a function is being called statically in PHP?

I meant if you call the method in this way class::method() or in this way $class->method() How do I know which way was called method in the same method?

Community
  • 1
  • 1
Abdo
  • 131
  • 1
  • 8
  • Heh, good finds guys. I'm flagging the two newer incarnations as a duplicate of the oldest one. – Pekka Oct 28 '12 at 17:45

1 Answers1

0

you shouldn't mix static with non-static methods while calling them, but you can check inside your method if exists $this.

$staticcalled = isset($this);
bukart
  • 4,906
  • 2
  • 21
  • 40
  • 3
    +1, but there shouldn't even have to be a way to check it. Like you said, don't mix static and non-static. It's a shame PHP even allows it. – GolezTrol Oct 28 '12 at 17:46
  • @GolezTrol: Not really. `function foo($bar) { $bar->Baz(); }` If I know there's a static `Baz` in any `$bar` I'm given, this works. Otherwise I have to know the concrete type, breaking duck typing. – Lightness Races in Orbit Oct 28 '12 at 17:48
  • "Mixing" static and non-static methods is not necessarily an indication of bad design; it might be just working around PHP's moronic insistence that you cannot call the same method both as static and as non-static (with `E_STRICT` in effect), _but_ also that you cannot have _two separate methods_ with the same name, one being static and the other non-static. – lanzz Oct 28 '12 at 17:49