The question is in the title.
+7
A:
They are non-static by default:
public function method() {
}
You will get an E_STRICT
if you call it statically, but I don't think you can easily enforce that it can only be called on an instance - if you try to check You could do $this
I think you will get an error.isset($this)
as Artefacto says and throw an exception if it isn't set.
Tom Haigh
2010-08-16 17:23:05
You can throw an exception if `isset($this)` is `false`.
Artefacto
2010-08-16 17:24:25
This can still be called as a static function.
Emanuil
2010-08-16 17:24:53
@Artefacto Have you seen any code doing this explicitly in all non-static methods? That looks cumbersome. Hopefully you will get some sort of error inside the method if $this is not set.
Alexandre Jasmin
2010-08-16 17:29:32
@Artefacto: thanks, tested and updated it. for some reason i thought that would fail.
Tom Haigh
2010-08-16 17:29:47
@Alex Yes, you get a fatal error if you use `$this` in a static context, which most non-static functions will do (if they don't access the instance, I suppose they could be static instead).
Artefacto
2010-08-16 17:31:17
+1
A:
<?php
class abc() {
public function foo() {
...
}
}
$c = new abc();
$c->foo();
?>
Aziz
2010-08-16 17:23:58
+1
A:
<?php
class abc() {
public function foo() {
if (!isset($this)) {
throw new Exception('Method is non-static.');
exit();
}
}
}
$c = new abc();
$c->foo();
?>
Not tested.
Nilks
2010-08-16 17:35:16