使用私有类函数/变量的 PHP 静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3357722/
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
PHP Static Method Using Private Class Functions/Variables
提问by Howard Zoopaloopa
If I write a public static method in a class ie...
如果我在一个类中编写一个公共静态方法,即......
public static function get_info($type){
switch($type){
case'title':
self::get_title();
break;
}
}
I have to write my get_title() function as public...
我必须将我的 get_title() 函数写为 public...
public static function get_title(){
return 'Title';
}
Otherwise I get the error:
否则我得到错误:
Call to private method Page::get_title()
Which makes me feel as though the function get_info()is essentially redundant. I'd like to be able to make a call from a static method to a private method inside my class for validation purposes. Is this impossible?
这让我觉得这个功能get_info()本质上是多余的。我希望能够从静态方法调用我的类中的私有方法以进行验证。这是不可能的吗?
PHP > 5.0 btw.
PHP > 5.0 顺便说一句。
!####### EDIT SOLUTION (BUT NOT ANSWER TO QUESTION) #########!
!####### 编辑解决方案(但不回答问题)#########!
In case you are curious, my workaround was to instantiate my static function's class inside the static function.
如果您好奇,我的解决方法是在静态函数内实例化我的静态函数的类。
So, the class name was Page I would do this...
所以,类名是 Page 我会这样做......
public static function get_info($type){
$page = new Page();
switch($type){
case'title':
$page->get_title();
break;
}
}
public function get_title(){
return 'Title';
}
采纳答案by Nicolas78
yes, it's impossible - a non-static method needs an object to read data from, while the point of a static one is that it has no such object attached. you can think of each non-static method of being passed an implicit argument, the object. you simply don't have a value to pass this value on to the method if you're calling from a static function.
是的,这是不可能的 - 非静态方法需要一个对象来读取数据,而静态方法的重点是它没有附加这样的对象。您可以认为每个非静态方法都传递了一个隐式参数,即对象。如果您从静态函数调用,您根本就没有将这个值传递给方法的值。
updateyou can have private static function - I'm not sure if your question might involve a slight misunderstanding of private and static as mutually exclusive concepts
更新您可以拥有私有静态函数 - 我不确定您的问题是否涉及对私有和静态作为互斥概念的轻微误解
回答by MrWhite
This is actually OK, there is nothing impossiblehere as far as I can see. Your static get_title()method can be private - or have I missed something? If both your static methods, get_info()and get_title(), are in the same class(whether it's staticor not) then your get_title()method can be private and your code still works without error. get_info()calls get_title()inside the class - statically. get_title()does not need to be public in your example, unless it needs to be accessible from outside that static class.
这实际上是可以的,据我所知,这里没有什么不可能的。您的静态get_title()方法可以是私有的 - 或者我错过了什么?如果您的静态方法get_info()和get_title()都在同一个类中(无论它是否是静态的),那么您的get_title()方法可以是私有的,并且您的代码仍然可以正常工作。类内部get_info()调用get_title()- 静态调用。get_title()在您的示例中不需要公开,除非需要从该静态类外部访问它。
Access (public, protected and private) applies to static classes (where all methods are static) as well as class instances.
访问(公共、受保护和私有)适用于静态类(其中所有方法都是静态的)以及类实例。
EDIT:You don't need to resort to instantiating the class in order to implement the private access...
编辑:您不需要为了实现私有访问而诉诸实例化类...
// Enable full error reporting to make sure all is OK
error_reporting(E_ALL | E_STRICT);
class MyStaticClass {
public static function get_info($type){
switch($type){
case 'title':
return self::get_title();
break;
}
}
private static function get_title() {
return 'Title';
}
}
// OK - get_info() calls the private method get_title() inside the static class
echo MyStaticClass::get_info('title');
// ERROR - get_title() is private so cannot be called from outside the class
echo MyStaticClass::get_title();

