问题
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?
回答1:
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.
回答2:
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.
回答3:
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
回答4:
Here's a good explanation: Karl Bunyan's blog on PHP5 Static classes
来源:https://stackoverflow.com/questions/8795651/when-to-use-static-classes-in-php