3

Possible Duplicate:
Is it possible to create static classes in PHP (like in C#)?

Can any one tell me if a php class can be declared as static ?

static class StaticClass
{
    public static function staticMethod()
    {
        return 'foo';
    }
}

This code giving me error.parse error: parse error, expecting `T_VARIABLE'

Community
  • 1
  • 1
Neelesh
  • 1,458
  • 2
  • 13
  • 20
  • Not an **exact** duplicate unless OP does want to strongly mimic C# static classes. – BoltClock Dec 17 '10 at 13:43
  • @BoltClock: but group [Can be done AND mimic C#] is just a subset of [Can be done], so that questions answers this one and goes more into it. – Adam Kiss Dec 17 '10 at 14:27
  • Not yet, ***may*** be possible in the future, since it was already suggested a few times - https://wiki.php.net/rfc/static-classes however I'm not sure what to think about "Status: in the works" and "Date: 2008-05-03" x) – jave.web Mar 03 '22 at 16:32

3 Answers3

6

No, you can't explicitly declare a PHP class as static.

You can make its constructor private so attempting to instantiate it (at least from outside the class) causes fatal errors.

class StaticClass
{
    private function __construct() {}

    public static function staticMethod()
    {
        return 'foo';
    }
}

// Fatal error: Call to private StaticClass::__construct() from invalid context
new StaticClass();

If you're looking to implement static initialization and other features found in C# static classes, see the other linked question. Otherwise if all you want is to group some utility methods into a class, simply privatizing the constructor should do the trick.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Thanks for you support,In this http://stackoverflow.com/questions/468642/is-it-possible-to-create-static-classes-in-php-like-in-c Answer 5 saying :You can have static classes in PHP.Is it true.I just want to know only this .Plz... – Neelesh Dec 17 '10 at 14:03
  • @Neelesh: You can write a class and treat it as if it were a static class, but you cannot write `static class` in PHP. – BoltClock Dec 17 '10 at 14:05
  • 2
    @Neelesh: Be sure to accept an answer by clicking the tick mark at the left. – BoltClock Dec 17 '10 at 14:12
3

One other alternative is to create the class as abstract. While it still can be extended and instantiated, it can't directly be.

abstract class test {
    public static function foo() {
    }
}

$foo = new test(); // Fatal error, can't instantiate abstract class

If you go with a private constructor, I'd suggest also making it final, since otherwise an extending class can override it and actually instantiate the class (as long as it doesn't call parent::__construct():

class test {
    private final function __construct() {}
}
class test2 extends test {
    public function __construct() {} // fatal error, can't extend final method
}
ircmaxell
  • 163,128
  • 34
  • 264
  • 314
0

You can declare a variable as static, and a method, but not a class:

static public $stat_sample = 'test';

static public getSample() {
 return "test";
}
taxman
  • 501
  • 11
  • 21