1

Possible Duplicate:
When to use static vs instantiated classes

I'm having a little trouble understanding the advantages/disadvantages of static vs "normal" classes in PHP, since it seems that I'm able to do the same thing with both.

If I'm able to have static variables in classes and get/modify them easily with static methods, why do I even need class instances?

Community
  • 1
  • 1
Rob
  • 13
  • 1
  • 1
  • 3
  • *(tip)* http://kore-nordmann.de/blog/0103_static_considered_harmful.html – Gordon Jan 09 '12 at 22:05
  • 2
    There's no such thing as a "static class", there are only classes which have `static` properties and/or methods. – deceze Jan 09 '12 at 22:34
  • It gets really confusing, but I guess this would help: http://www.php5-tutorial.com/classes/static-classes/ – Omar Feb 18 '15 at 21:15

4 Answers4

8

Are you sure you don't mean abstract class? PHP has abstract classes and static methods.

An abstract class provides you with a mechanism to build an object with a generalized API that can be specialized to various other objects that are subclasses of it, but for which it doesn't make sense for an instance of the generalized class to exist. For example, if you were constructing a system that managed animals, then you may have classes for specific animals such as meerkat, ferret, gecko, snake, fish and so on. Some of the animals in the system can be grouped together by common characteristics. For example, all the animals mentioned are vertebrate, so you might have a vertebrate class which describes the characteristics common to all the animals that can be categorized as a vertebrate.

However, there is no such animal as a vertebrate, so you shouldn't be able to have an instance of the Vertebrate class. You can have instances of ferrets and snakes, and those instances should have all the characteristics of vertebrates but a vertebrate instance would make no sense. You can further subclass of course, you might have a mammal and a reptile class which sit between vertebrate and the specific animals, but which are also abstract and cannot have instances.

Basically, you should think of an abstract class as a way of defining the general behaviour of a class of objects that might be derived from it.

Sorry if I've not explained myself very well, it's a much simpler concept to understand than it is to explain.

If, on the other hand, you're talking about classes that contain nothing but static methods, then that's simply a way for a programmer to delude himself into believing that the procedural code he's writing is "object oriented programming". It's not, it's just a way of disguising procedural programming.

There are schools of thought that frown on static methods as they can make testing sections of code in isolation very difficult. While they do have their uses, it's generally advisable to avoid static methods.

GordonM
  • 31,179
  • 15
  • 87
  • 129
  • As far as I know, any classing having at least one static method or property is known as static class. – Adnan Apr 06 '18 at 20:53
5

The Static instance of a class only happens once, and its variables are available to any instance of the class. Instances have their own individual values which are not accessible to other instances, except where they are marked static.

Instances are useful when there will be more than one instance of the class.

Tim
  • 14,447
  • 6
  • 40
  • 63
  • Oh, cool. So if I have an autoloader class, and the only value it holds is an array of classes (with a method to merge values to that array), that would be a good use of static values/methods? There would be no need to have multiple instances for this. – Rob Jan 09 '12 at 22:08
5

A static class does not need to be instanced with the new operator. It is always usable whereas a "normal" class has to be instanced.

The instanced class may have many instances, the static one has only one "instance".

class StaticHello {
  static protected $sProperty = 'static';
  static public function sayHello()
  {
    echo 'Hello, I am ' . self::$sProperty;
  }
}

class InstancedHello {
  protected $sProperty;
  public function __construct($name)
  {
    $this->sProperty = $name;
  }
  public function sayHello()
  {
    echo 'Hello, I am ' . $this->sProperty;
  }
}

StaticHello::sayHello();
// outputs "Hello, I am static"

$oInstance1 = new InstancedHello('Rob');
$oInstance2 = new InstancedHello('Fbableus');
$oInstance1->sayHello();
// outputs "Hello, I am Rob"
$oInstance2->sayHello();
// outputs "Hello, I am Fbableus"

Note that instanced classes may have static properties and methods shared by all instances and accessible by the :: operator

fbableus
  • 133
  • 5
2

Here's a good explanation: Karl Bunyan's blog on PHP5 Static classes

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Mino
  • 911
  • 1
  • 7
  • 14