PHP 中的静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9206760/
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
Static methods in PHP
提问by donRumatta
Why in PHP you can access static method via instance of some class but not only via type name?
为什么在 PHP 中您可以通过某个类的实例访问静态方法,而不仅仅是通过类型名称?
UPDATE: I'm .net developer but i work with php developers too. Recently i've found this moment about static methods called from instance and can't understand why it can be usefull.
更新:我是 .net 开发人员,但我也与 php 开发人员一起工作。最近我发现这个时刻是关于从实例调用的静态方法,但不明白为什么它会有用。
EXAMPLE:
例子:
class Foo
{
public static Bar()
{
}
}
We can accept method like this:
我们可以接受这样的方法:
var $foo = new Foo();
$foo.Bar(); // ??????
回答by Ibrahim Azhar Armar
In PHP
在 PHP 中
the class is instantiated using the new keyword for example;
例如,使用 new 关键字实例化该类;
$MyClass = new MyClass();
$MyClass = new MyClass();
and the static method or properties can be accessed by using either scope resolution operator or object reference operator. For example, if the class MyClass
contains the static method Foo()
then you can access it by either way.
并且可以使用范围解析运算符或对象引用运算符访问静态方法或属性。例如,如果类MyClass
包含静态方法,Foo()
那么您可以通过任何一种方式访问它。
$MyClass->Foo();
Or
或者
MyClass::Foo()
The only rule is that static methods or properties are out of object context. For example, you cannot use $this
inside of a static method.
唯一的规则是静态方法或属性在对象上下文之外。例如,您不能$this
在静态方法内部使用。
回答by Said Afkir
Class Do {
static public function test() {
return 0;
}
}
use like this :
像这样使用:
echo Do::test();
回答by Matthew
Why in PHP you can access static method via instance of some class but not only via type name?
为什么在 PHP 中您可以通过某个类的实例访问静态方法,而不仅仅是通过类型名称?
Unlike what you are probably used to with .NET, PHP has dynamic types. Consider:
与您可能习惯的 .NET 不同,PHP 具有动态类型。考虑:
class Foo
{
static public function staticMethod() { }
}
class Bar
{
static public function staticMethod() { }
}
function doSomething($obj)
{
// What type is $obj? We don't care.
$obj->staticMethod();
}
doSomething(new Foo());
doSomething(new Bar());
So by allowing access to static methods via the object instance, you can more easily call a static function of the same name across different types.
因此,通过允许通过对象实例访问静态方法,您可以更轻松地跨不同类型调用同名静态函数。
Now I don't know if there is a good reason why accessing the static method via ->
is allowed. PHP (5.3?) also supports:
现在我不知道是否有充分的理由->
允许通过访问静态方法。PHP (5.3?) 还支持:
$obj::staticMethod();
which is perhaps less confusing. When using ::
, it must be a static function to avoid warnings (unlike ->
, which permits either).
这可能不那么令人困惑。使用时::
,它必须是一个静态函数以避免警告(不像->
,它允许)。
回答by Web Dev guy
In PHP, while you're allowed to access the static method by referencing an instance of the class, you don't necessarily need to do so. For example, here is a class with a static function:
在 PHP 中,虽然您可以通过引用类的实例来访问静态方法,但您不一定需要这样做。例如,这是一个带有静态函数的类:
class MyClass{
public static function MyFunction($param){
$mynumber=param*2;
return $mynumber;
}
You can access the static method just by the type name like this, but in this case you have to use the double colon (::), instead of "->".
您可以像这样通过类型名称访问静态方法,但在这种情况下,您必须使用双冒号 (::),而不是“->”。
$result= MyClass::MyFunction(2);
(Please note you can also access the static method via an instance of the class as well using "-->"). For more information: http://php.net/manual/en/language.oop5.static.php
(请注意,您也可以通过类的实例访问静态方法,也可以使用“-->”)。更多信息:http: //php.net/manual/en/language.oop5.static.php
回答by ryh
In PHP 7 it seems to be absolutely necessary for you to be able to do $this->staticFunction()
. Because, if this code is written within an abstract class and staticFunction()
is also abstract in your abstract class, $this->
and self::
deliver different results!
在 PHP 7 中,您似乎绝对需要能够做到$this->staticFunction()
. 因为,如果这个代码是一个抽象类中的书面和staticFunction()
也是抽象的在你的抽象类,$this->
并self::
提供不同的结果!
When executing $this->staticFunction()
from a (non-abstract) child of the abstract class, you end up in child::staticFunction()
. All is well.
当$this->staticFunction()
从抽象类的(非抽象)子类执行时,您最终会在child::staticFunction()
. 一切都很好。
However, executing self::staticFunction()
from a (non-abstract) child of the abstract class, you end up in parent::staticFunction()
, which is abstract, and thusly throws an exception.
但是,self::staticFunction()
从抽象类的(非抽象)子级执行,您最终会进入parent::staticFunction()
,这是抽象的,因此会引发异常。
I guess this is just another example of badly designed PHP. Or myself needing more coffee...
我想这只是设计糟糕的 PHP 的另一个例子。或者我自己需要更多的咖啡...
回答by Mahedi Hasan Durjoy
Sometimes, it is useful if we can access methods and properties in the context of a class rather than an object. To do this, you can use static keyword. So we can access static method using class name along with scope resolution .
有时,如果我们可以在类而不是对象的上下文中访问方法和属性,这会很有用。为此,您可以使用 static 关键字。所以我们可以使用类名和作用域解析来访问静态方法。
class User
{
public static $name = 'john';
public static function display()
{
return self::$name;
}
}
echo User::display();
Look here we declared a static method in our User class . So if we declare a static method then we can access there using just class name . No need to create object to access there . Hope you will understand all the procedure .
看这里,我们在 User 类中声明了一个静态方法。所以如果我们声明一个静态方法,那么我们可以只使用类名访问那里。无需创建对象即可访问那里。希望你能理解所有的程序。
Note : This is actually the main difference with a normal method : $this is not available in such methods.
注意:这实际上是与普通方法的主要区别:$this 在此类方法中不可用。
read full article from here How to Use Static Properties in PHP ?
从这里阅读全文如何在 PHP 中使用静态属性?