tags:

views:

85

answers:

3

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 $this I think you will get an error. You could do isset($this) as Artefacto says and throw an exception if it isn't set.

Tom Haigh
You can throw an exception if `isset($this)` is `false`.
Artefacto
This can still be called as a static function.
Emanuil
@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
@Artefacto: thanks, tested and updated it. for some reason i thought that would fail.
Tom Haigh
@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
+1  A: 
<?php
class abc() {

 public function foo() {
     ...
 }
}

$c = new abc();
$c->foo();
?>
Aziz
foo() would still be available through abc::foo().
Emanuil
yes that's true. I don't know another way to do it. Function is public and object oriented (means that's a method)
Aziz
+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