php 私有静态方法与静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11496884/
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
Private static method vs. static method
提问by MacGruber
I understand that staticmeans that an object doesn't need to be instantiated for that property/method to be available. I also understand how this applies to private properties and methods and public methods. What I'm trying to understand is what static private function gains you. For example:
我理解这static意味着不需要为该属性/方法实例化一个对象即可使用。我也了解这如何适用于私有属性和方法以及公共方法。我想了解的是静态私有函数为您带来了什么。例如:
class Beer {
static private $beertype = "IPA";
private function getBeerType() {
return self::$beertype;
}
static public function BeerInfo() {
return self::getBeerType();
}
}
print Beer::BeerInfo() . "\n";
The private method getBeerType()executes just fine without an instantiated object as long as it's being called from a static public method. If a static public method has access to all private methods (static and non-static), what's the benefit of declaring a method static private?
getBeerType()只要从静态公共方法调用私有方法,就可以在没有实例化对象的情况下正常执行。如果静态公共方法可以访问所有私有方法(静态和非静态),那么将方法声明为静态私有有什么好处?
With strict error reporting turned on, I do get the warning that I shouldmake getBeerType()static, although it still lets me run the code. And I did a little research and it seems like other languages (Java) will force you to declare a private method as static when called by a static public method. Looks like PHP lets you get away with this. Is there a way to force it to throw an error and not execute?
启用严格的错误报告后,我确实收到了我应该将其getBeerType()设为静态的警告,尽管它仍然允许我运行代码。我做了一些研究,似乎其他语言(Java)会强制您在被静态公共方法调用时将私有方法声明为静态方法。看起来 PHP 可以让你摆脱这个。有没有办法强制它抛出错误而不执行?
回答by staticsan
A static private method provides a way to hide static code from outside the class. This can be useful if several different methods (static or not) need to use it, i.e. code-reuse.
静态私有方法提供了一种从类外部隐藏静态代码的方法。如果几种不同的方法(静态或非静态)需要使用它,即代码重用,这可能很有用。
Static methods and static variables, sometimes called class methods and class variables, are a way of putting code and data into a kind of namespace. You could also think of class variables as variables attached to the class itself, of which there is (by definition) exactly one, instead of to instances of that class, of which there may be zero, one or many. Class methods and class variables can be useful in working with attributes that not just remain same in all instances, but actually bethe same.
静态方法和静态变量,有时称为类方法和类变量,是将代码和数据放入某种命名空间的一种方式。您还可以将类变量视为附加到类本身的变量,其中(根据定义)只有一个,而不是该类的实例,其中可能有零个、一个或多个。类方法和类变量可以在与不只是在所有情况下保持相同的属性时非常有用,但实际上是相同的。
An example of a class variable is a database handler in an ORM entity object. All instances are their own object, but they all need access to the samedatabase handler for loading and saving themselves.
类变量的一个示例是 ORM 实体对象中的数据库处理程序。所有实例都是它们自己的对象,但它们都需要访问相同的数据库处理程序来加载和保存它们自己。
Private versus public is a completely separate quality, which is I suspect what you're stumbling over. A private method cannot be called and private variables cannot be accessed from code outside the class. Private methods are usually used to implement "internal" logic on the object that must not be accessible from outside the object. This restriction can be needed by instance methods as well as class methods.
私人与公共是完全不同的品质,我怀疑这是您绊倒的东西。不能调用私有方法,不能从类外的代码访问私有变量。私有方法通常用于在对象上实现不能从对象外部访问的“内部”逻辑。实例方法和类方法都可能需要此限制。
An example of a private class method could be in a factory method. There might be three factory calls for creating an object which might differ in parameters being supplied. Yet the bulk of the operation is the same. So it goes into a private staticmethod that the non-private factory methods call.
私有类方法的一个例子可能是在工厂方法中。可能存在三个用于创建对象的工厂调用,它们提供的参数可能不同。然而,大部分操作是相同的。所以它进入了一个private static非私有工厂方法调用的方法。
回答by hakre
I understand static means that an object doesn't need to be instantiated for that property/method to be available.
我理解静态意味着不需要为该属性/方法实例化一个对象即可使用。
Everything static just exists. Globally.
一切都是静态的。全球。
I also understand how this applies to public properties and methods and public methods
我也明白这如何适用于公共属性和方法以及公共方法
Are you sure you have understood that it creates a global variable and a standard global function?
你确定你明白它创建了一个全局变量和一个标准的全局函数吗?
What I'm trying to understand is what static private function gains you.
我想了解的是静态私有函数为您带来了什么。
The privateis just a specifier of visibilityDocs. So that gains you visibility control.
该私人仅仅是一个符可见性文件。这样您就可以获得可见性控制。
Is it useful? Depends on the use-case.
有用吗?取决于用例。
回答by Xavi Montero
it's for preventing OTHERS from consuming it.
这是为了防止其他人食用它。
Example, you have a Loggerstatic object, then you have two public static methods LogOkand LogErrorand both benefeit from an "internal" method Logbut you don't want the consumers of that class to be able to call Logdirectly.
例如,您有一个Logger静态对象,然后您有两个公共静态方法LogOk,LogError并且都受益于“内部”方法,Log但您不希望该类的使用者能够Log直接调用。
You can call Logger::LogOk( "All right." );but you cannot call Logger::Log( "abc" );if Logis private.
你可以打电话,Logger::LogOk( "All right." );但Logger::Log( "abc" );如果Log是私人的,你就不能打电话。
You you can internally always make use of it from the same class.
你可以在内部总是从同一个类中使用它。
回答by d.raev
Simply said you can declare a private static functionif you have a repeated operation in some of the public staticfunctions in the class.
简单地说,private static function如果您public static在类中的某些函数中有重复操作,则可以声明 a 。
Naturally if you are an inexperienced programmer or new to the OOP putting limitationsto your code seem strange. But strict declarations like this will make your code cleaner and easier to maintain. In large projects and complex classes you can appreciate to know exactlywhat to expect from a function and exactlyhow you can use it.
自然地,如果您是一个没有经验的程序员或 OOP 新手,那么对您的代码进行限制似乎很奇怪。但是像这样的严格声明将使您的代码更清晰,更易于维护。在大型项目和复杂的类中,您可以准确地知道从函数中得到什么以及如何使用它。
Here is a good read: Single responsibility principleand God Object
回答by Aurelio De Rosa
Although the code works, it throws a Strict standards error:
尽管代码有效,但它抛出了严格的标准错误:
Strict standards: Non-static method Beer::getBeerType() should not be called statically
严格标准:非静态方法 Beer::getBeerType() 不应静态调用
So, here you get the use of the private static.
所以,在这里你可以使用private static.
回答by Jonh Doe
Here's the rule and the best answer,
这是规则和最佳答案,
static methods cannot access non-static variables and methods, since these require an instance of the class. Don't worry about the warning, the rule is set and it will break your code in the future once it's fully enforced. That is why
静态方法不能访问非静态变量和方法,因为它们需要类的实例。不要担心警告,规则已经设置,一旦完全执行,它将在将来破坏您的代码。这就是为什么
static public function BeerInfo() {
return self::getBeerType()
is wrong,
是错的,
you have to declare getBeerType as static.
您必须将 getBeerType 声明为静态。
回答by Ryan Gentner
In your example, you can simplify this by doing the following.
在您的示例中,您可以通过执行以下操作来简化此操作。
static private $beertype = "IPA";
static public function BeerInfo() {
return self::$beertype;
}
'static' purely means resident in a single region of memory. If you are memory conscious, static implementations are a good strategy.
“静态”纯粹是指驻留在单个内存区域中。如果您有记忆意识,静态实现是一个很好的策略。
When you use a public static function, chances are, most of the time, that you don't want to deal with an instance of that class, but want to re-use pre-existing functionality from within that class. Leveraging private static functions is the way to do that without instances.
当您使用公共静态函数时,大多数情况下,您可能不想处理该类的实例,而是想重新使用该类中预先存在的功能。利用私有静态函数是在没有实例的情况下做到这一点的方法。
However, you could have a public static function which accepts an argument which is an instance of said class, e.g.
但是,您可以有一个公共静态函数,它接受一个参数,该参数是所述类的实例,例如
static public function doSomething(Beer &$ref) {
$ref->instanceLevelFunction(...);
}

