1

I guess my question is would

static class example1{
    function example1_function(){};     
}

and

class example2{
    static function example2_function(){};
}

lead to the same result, which is both example1->example1_function() and example2->example2_function() have the same callabilty. Would both be defined as static and usable as such?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Brook Julias
  • 2,085
  • 9
  • 29
  • 44
  • 1
    The `static class` and the `class class` will lead to parse errors. – mario May 06 '11 at 21:31
  • Take a look on this: [link](http://stackoverflow.com/questions/468642/is-it-possible-to-create-static-classes-in-php-like-in-c) – Damien May 06 '11 at 21:31

3 Answers3

3

PHP does not allow you to declare a class static.

To call a static method, you must use the :: operator.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
2

You cannot declare a class as a static class, as stated by other members here, but there is a method where you can stop the class from becoming an object, you can use the abstract keyword to specify the object should not be instantiated using the new keyword, this is good for inheritance etc.

abstract class Something
{
}

Doing new Something would trigger an error stating you cannot instantiate the class, you can then declare your static methods like so:

abstract class Something
{
    public static function Else()
    {
    }
}

You still have to declare you methods as static, this is just the way it is.

and then you can use like so:

Something::Else();

Hope this clears up a few thing's

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
0

As has already been mentioned in the comments, the static keyword is not used for classes in that way (syntax).

http://php.net/manual/en/language.oop5.static.php

Thomas Langston
  • 3,743
  • 1
  • 25
  • 41