class StaticMethod
{
public $a=10;
public $b=20;
public static function sum(){
return (self::$a+self::$b);
}
}
echo StaticMethod::sum();
Asked
Active
Viewed 811 times
-2

u_mulder
- 54,101
- 5
- 48
- 64

Selvesan Malakar
- 511
- 2
- 7
- 20
-
3You declared `$a` without `static`. What do you expect? – u_mulder Dec 28 '15 at 18:47
1 Answers
0
You declared your sum
method as static, but you didn't declare your variables $a
and $b
as static.
class StaticMethod
{
public static $a = 10;
public static $b = 20;
public static function sum() {
return (self::$a + self::$b);
}
}
echo StaticMethod::sum(); //returns 30

romellem
- 5,792
- 1
- 32
- 64
-
what if i want those variable as it is i.e not static. But want to access them in the function sum – Selvesan Malakar Dec 28 '15 at 18:52
-
You cannot access non-static properties from a static method. See this answer - http://stackoverflow.com/a/15118512/864233 - for more details on that. – romellem Dec 28 '15 at 18:55
-