私有静态方法与静态方法

时间:2023-01-11 16:54:57

I understand that static means 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:

我理解静态意味着不需要实例化对象以使该属性/方法可用。我也理解这如何适用于私有属性和方法以及公共方法。我想要了解的是静态私有函数会让你获益。例如:

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 should make 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让你逃脱这一点。有没有办法强制它抛出错误而不执行?

7 个解决方案

#1


28  

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 be the 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 same database 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 static method that the non-private factory methods call.

私有类方法的示例可以是工厂方法。可能有三个工厂调用用于创建可能与提供的参数不同的对象。然而,大部分操作都是一样的。所以它进入非私有工厂方法调用的私有静态方法。

#2


6  

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 private is just a specifier of visibilityDocs. So that gains you visibility control.

private只是visibilityDocs的说明符。这样可以获得可视性控制。

Is it useful? Depends on the use-case.

它有用吗?取决于用例。

#3


4  

it's for preventing OTHERS from consuming it.

这是为了防止其他人消费它。

Example, you have a Logger static object, then you have two public static methods LogOk and LogError and both benefeit from an "internal" method Log but you don't want the consumers of that class to be able to call Log directly.

例如,你有一个Logger静态对象,那么你有两个公共静态方法LogOk和LogError,它们都来自“内部”方法Log但你不希望该类的使用者能够直接调用Log。

You can call Logger::LogOk( "All right." ); but you cannot call Logger::Log( "abc" ); if Log is private.

你可以调用Logger :: LogOk(“好吧。”);但你不能调用Logger :: Log(“abc”);如果Log是私有的。

You you can internally always make use of it from the same class.

你可以在内部总是从同一个类中使用它。

#4


3  

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.

所以,在这里你可以使用私有静态。

#5


2  

Simply said you can declare a private static function if you have a repeated operation in some of the public static functions in the class.

简单地说,如果在类中的某些公共静态函数中重复操作,则可以声明私有静态函数。

Naturally if you are young programmer or new to the OOP putting limitations to your code seem strange. But strict declarations like this will make your cleaner and easier to maintain. In large projects and complex classes you can appreciate to know exactly what to expect from a function and exactly how you can use it.

当然,如果你是年轻的程序员或OOP的新手,那么对你的代码的限制似乎很奇怪。但是像这样的严格声明将使您更清洁,更容易维护。在大型项目和复杂类中,您可以欣赏到确切地知道函数的内容以及您如何使用它。

Here is a good read: Single responsibility principle and God Object

这是一个很好的阅读:单一责任原则和上帝对象

#6


1  

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声明为static。

#7


1  

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.

'static'纯粹意味着居住在单一的记忆区域。如果你有记忆意识,静态实现是一个很好的策略。

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(...);
}

#1


28  

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 be the 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 same database 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 static method that the non-private factory methods call.

私有类方法的示例可以是工厂方法。可能有三个工厂调用用于创建可能与提供的参数不同的对象。然而,大部分操作都是一样的。所以它进入非私有工厂方法调用的私有静态方法。

#2


6  

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 private is just a specifier of visibilityDocs. So that gains you visibility control.

private只是visibilityDocs的说明符。这样可以获得可视性控制。

Is it useful? Depends on the use-case.

它有用吗?取决于用例。

#3


4  

it's for preventing OTHERS from consuming it.

这是为了防止其他人消费它。

Example, you have a Logger static object, then you have two public static methods LogOk and LogError and both benefeit from an "internal" method Log but you don't want the consumers of that class to be able to call Log directly.

例如,你有一个Logger静态对象,那么你有两个公共静态方法LogOk和LogError,它们都来自“内部”方法Log但你不希望该类的使用者能够直接调用Log。

You can call Logger::LogOk( "All right." ); but you cannot call Logger::Log( "abc" ); if Log is private.

你可以调用Logger :: LogOk(“好吧。”);但你不能调用Logger :: Log(“abc”);如果Log是私有的。

You you can internally always make use of it from the same class.

你可以在内部总是从同一个类中使用它。

#4


3  

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.

所以,在这里你可以使用私有静态。

#5


2  

Simply said you can declare a private static function if you have a repeated operation in some of the public static functions in the class.

简单地说,如果在类中的某些公共静态函数中重复操作,则可以声明私有静态函数。

Naturally if you are young programmer or new to the OOP putting limitations to your code seem strange. But strict declarations like this will make your cleaner and easier to maintain. In large projects and complex classes you can appreciate to know exactly what to expect from a function and exactly how you can use it.

当然,如果你是年轻的程序员或OOP的新手,那么对你的代码的限制似乎很奇怪。但是像这样的严格声明将使您更清洁,更容易维护。在大型项目和复杂类中,您可以欣赏到确切地知道函数的内容以及您如何使用它。

Here is a good read: Single responsibility principle and God Object

这是一个很好的阅读:单一责任原则和上帝对象

#6


1  

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声明为static。

#7


1  

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.

'static'纯粹意味着居住在单一的记忆区域。如果你有记忆意识,静态实现是一个很好的策略。

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(...);
}