tags:

views:

171

answers:

5

Instead of declaring each function as "static" in a class, is there any way that I can make a class itself "static"?

+5  A: 

Not in PHP - you must mark every member as static that you wish to be static. For for information please see the PHP manual.

Andrew Hare
A: 

You can have a look to this page but I don't think you can declare the entire class as static.

mnml
@mnml: Already seen that page before posting. Thanks anyway.
RPK
A: 

Or if what you really want to achieve is to only have one class instance, then make the constructor private and provide with a static instance() method. For example:

class PseudoStatic {

  static private $instance;

  private function __construct() {}

  static public function instance() {
    if (!self::$instance) {
      self$instance = new self;
    }
    return self::$instance;
  }

}

$instance = new PseudoStatic(); // error!
$instance = PseudoStatic::instance(); // force one instance only
Lukman
This more a singleton like pattern and has nothing to do with static classes ...
Mario Mueller
This is the Singleton pattern, which is not the same as Static
iAn
well, i'm suggesting a workaround since static class has not really existed yet in php5. maybe php6 or php7? for me, as long as a suggestion solve the problem, it doesn't really have to answer the question ;)
Lukman
PHP won't need static classes, but this a question for the evangelists ;)
Mario Mueller
@Mario: well, aren't you suggesting the same thing????! -______-
Lukman
I believe Mario is suggesting referencing members of the static class as StaticClassName::StaticMethodName();
Cory House
@Cory - That point is for you ;)@Lukman - The singleton points to a "one-instance" concept in a global application or domain scope. My comment points to a "no-instance-at-all" way of providing an encapsulated version of a function library.
Mario Mueller
+4  A: 

There is nothing comparable to the (for ex.) Java static classes way. If you just want to collect function in a kind of library, you can set the __construct() and the __clone() methods to private. This will prevent the creation of instances.

Mario Mueller
+2  A: 

I'd say the best way to go is to prevent object instantiation through a private constructor and explicitly marking all methods as static. Although you have to be careful to mark all methods as static (which is the result of static classes not existing in PHP), the benefit of this method over the Singleton approach is that static methods are more efficient than their non-static counterparts. You probably also want your class marked as final, as most static classes are not designed to be extended anyway (and it is good practice to do so).

An example would be something like this:

final class PseudoStatic {

  /**
   * Prevent object instantiation
   */
  private function __construct() {}

  static public function method1() {
    ...
  }

  static public function method2() {
    ...
  }

  ...
}

Furthermore, the Singleton pattern is now considered a bad practice by some.

ErikSchierboom