Reputation: 18046
When calling a static method in PHP, can one tell if the method is running in an instantiated object or not?
Something like
public static function update( $value = NULL) {
if ( self::is_instantiated() ) {
update_db($this->value);
} else {
update_db($value);
}
}
I don't want to simply test ! is_null($value)
because the method can be called publicly without passing a value.
I tried checking isset()
for $this
and $this->property
, but that didn't work in the non-object context.
Upvotes: 3
Views: 171
Reputation: 5037
Just delete the static
keyword. Static method is always static.
class A {
public function whoami() {
echo $this ? "i am object\n" : "i am class\n";
}
}
$a = new A();
$a->whoami();
A::whoami();
Result:
i am object
i am class
Upvotes: 3