什么时候在 PHP 中使用静态类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8795651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use static classes in PHP?
提问by Rob
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.
我在理解 PHP 中静态类与“普通”类的优缺点时遇到了一些麻烦,因为我似乎可以对两者做同样的事情。
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?
如果我能够在类中拥有静态变量并使用静态方法轻松获取/修改它们,为什么我什至需要类实例?
回答by GordonM
Are you sure you don't mean abstract class? PHP has abstract classes and static methods.
你确定你说的不是抽象类吗?PHP 有抽象类和静态方法。
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.
抽象类为您提供了一种机制来构建具有通用 API 的对象,该 API 可以专门用于作为它的子类的各种其他对象,但对于这些对象,通用类的实例的存在没有意义。例如,如果您正在构建一个管理动物的系统,那么您可能有针对特定动物的类,例如猫鼬、雪貂、壁虎、蛇、鱼等。系统中的一些动物可以根据共同特征组合在一起。例如,提到的所有动物都是脊椎动物,因此您可能有一个脊椎动物类,它描述了所有可以归类为脊椎动物的动物的共同特征。
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.
但是,没有脊椎动物这样的动物,因此您不应该拥有 Vertebrate 类的实例。你可以有雪貂和蛇的实例,这些实例应该具有脊椎动物的所有特征,但脊椎动物实例没有意义。当然,您可以进一步子类化,您可能有一个哺乳动物和一个爬行动物类,它们位于脊椎动物和特定动物之间,但它们也是抽象的,不能有实例。
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.
有些流派不赞成静态方法,因为它们会使单独测试代码部分变得非常困难。尽管它们确实有其用途,但通常建议避免使用静态方法。
回答by fbableus
A staticclass does not need to be instanced with the newoperator. 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
请注意,实例化类可能具有所有实例共享的静态属性和方法,并可通过 :: 运算符访问
回答by Tim
The Static
instance of a class only happens once, and its variables are available to anyinstance of the class. Instances have their own individual values which are not accessible to other instances, except where they are marked static
.
Static
一个类的实例只发生一次,它的变量可用于该类的任何实例。实例有自己的单独值,其他实例无法访问这些值,除非它们被标记为static
。
Instances are useful when there will be more than oneinstance of the class.
当类的实例不止一个时,实例很有用。
回答by Mino
Here's a good explanation: Karl Bunyan's blog on PHP5 Static classes
这是一个很好的解释:Karl Bunyan 关于 PHP5 静态类的博客