PHP中类的静态函数和函数有什么区别?

时间:2023-01-15 17:43:39

I need to have method to get something from a database, but I don't understand the difference between static and normal functions in PHP.

我需要有从数据库中获取内容的方法,但我不理解PHP中静态函数和普通函数之间的区别。

Example code

示例代码

class Item {
    public static function getDetail($arg) {
        $detail = $this->findProductId($arg);   
        return $detail;
    }

    private function findProductId($id) {
        //find product_id in database where id = $arg
        //return detail of product
    }
}

and function outside of a class

和课外的功能

function getDetail($arg) {
    $detail = findProductId($arg);
    return $detail;
}

If I use $item = Item::getDetail(15); and $item = getDetail(15); — they're the same.

如果我使用$ item = Item :: getDetail(15);和$ item = getDetail(15); - 他们是一样的。

  1. What is the difference between static and function outside of a class?
  2. 一个类之外的静态和函数有什么区别?
  3. If they are difference, How to use static function and function outside of a class? (I'd appreciate a really simple example.)
  4. 如果它们不同,如​​何在类之外使用静态函数和函数? (我很欣赏一个非常简单的例子。)
  5. What are the performance properties between static and function outside of a class? Which is better?
  6. 类之外的静态和函数之间的性能属性是什么?哪个更好?

6 个解决方案

#1


10  

1) What is difference between static function and normal function

1)静态功能和正常功能有什么区别

While they are functions, I'd prefer to call them methods of a given class. One is a static method and the other is an instance method.

虽然它们是函数,但我更喜欢称它们为给定类的方法。一个是静态方法,另一个是实例方法。

Static Method: $item = Item::getDetail(15);

静态方法:$ item = Item :: getDetail(15);

Instance Method: $item = getDetail(15);

实例方法:$ item = getDetail(15);

(refer to FuzzyTree's correct syntax above in the comments, however.)

(但是,请参阅上面评论中的FuzzyTree的正确语法。)

2) How to use static function and normal function (if you simple exemplify is good)

2)如何使用静态函数和正常函数(如果你简单的例子是好的)

Static means you do not have to instantiate (declare an object reference). That is, you can simply use the method. So, in your example, while the answer may be the same, the way you called that method/function is different, as you noted above.

静态意味着您不必实例化(声明对象引用)。也就是说,您可以简单地使用该方法。因此,在您的示例中,虽然答案可能相同,但您调用该方法/函数的方式也不同,如上所述。

In Java, for example, you have the Math class. It does not require instantiation to use, and in fact you cannot that I know of because its constructor is private. You can simply use a method by referencing the class and the method name you wish to use,

例如,在Java中,您拥有Math类。它不需要实例化使用,事实上你不能我所知道的,因为它的构造函数是私有的。您可以通过引用要使用的类和方法名称来简单地使用方法,

Math.pow(d1, d2);  //no instantiation needed

in PHP this might be,

在PHP中这可能是,

MyClass::pow(d1,d2); //no instantiation needed

Java: when to use static methods

Java:何时使用静态方法

3) Ask performance between static function and normal function. Which is better?

3)询问静态功能和正常功能之间的性能。哪个更好?

Better is a matter of your design. If you have to create an object every time you want to do the power of a number that will create more memoryusage than simply using the class directly. I do not have benchmark proof, but it seem logical since you are not handling the method the same way in memory. I do not think it will matter in real world unless you are doing a lot of complicated actions.

更好的是你的设计问题。如果你每次想要使用一个数字的力量来创建一个对象,那么就会产生更多的记忆,而不是简单地直接使用这个类。我没有基准证明,但它似乎合乎逻辑,因为你没有在内存中以相同的方式处理方法。除非你做了很多复杂的动作,否则我认为它在现实世界中并不重要。

Performance of static methods vs instance methods

静态方法与实例方法的性能

might also interest you.

也许你会感兴趣。

#2


2  

I'm assuming you're asking about the difference between a static method:

我假设你在询问静态方法之间的区别:

class Item {
    public static function getDetail($arg){}
}

And a function written outside of a class definition:

并且在类定义之外编写的函数:

function getDetail($arg){}

Static methods should be used over functions written outside of a class for organizational reasons. In an application with many files, having Item::getDetails($arg) will give a hint of where that function is defined. Also, having just a function written outside of a class runs the risk of name collision, if you start writing many functions outside classes.

出于组织原因,静态方法应该用于在类外部编写的函数。在具有许多文件的应用程序中,具有Item :: getDetails($ arg)将提示该函数的定义位置。此外,如果您开始在类之外编写许多函数,那么只有在类外部编写的函数会冒名称冲突的风险。

Especially if you are writing in the OOP style, you should be using static methods over functions outside of class definitions, but I think even in general, using static methods is the better way to go.

特别是如果你用OOP风格编写,你应该使用静态方法而不是类定义之外的函数,但我认为即使一般来说,使用静态方法也是更好的方法。

#3


1  

1.Entire difference is, you don't get $this supplied inside the static function. If you try to use $this, you'll get a Fatal error: Static functions are associated with the class, not an instance of the class and You will get an E_strict warning in the second case.

1.完全不同的是,你不会在静态函数中得到$ this。如果你试图使用$ this,你会得到致命错误:静态函数与类相关联,而不是类的实例,在第二种情况下你会收到E_strict警告。

2.Now static calling of non-static methods works but is deprecated. so use normal function.

2.现在静态调用非静态方法有效但不推荐使用。所以使用正常功能。

3.Technically speaking Static methods and variables are useful when you want to share information between objects of a class, or want to represent something that's related to the class itself, not any particular object. For some reason, PHP allows you to call non-static and static methods interchangeably,So performance wise or in whatever way you may say just use a normal function.Static is not going to help you at all

3.技术上讲静态方法和变量在你想要在类的对象之间共享信息,或者想要表示与类本身相关的东西而不是任何特定对象时非常有用。出于某种原因,PHP允许您交替地调用非静态和静态方法,因此性能明智或以任何方式您可以说只使用正常函数.Static根本不会帮助您

#4


1  

The difference between static and non static is that you can access a static method without having an instance of the class.

静态和非静态之间的区别在于您可以在没有类实例的情况下访问静态方法。

$item = getDetail(15); shouldn't work because detDetail is a method in a class and not a function.

$ item = getDetail(15);不应该工作因为detDetail是类中的方法而不是函数。

Performance of Static Methods

静态方法的性能

There used to be a big penalty when calling a static method - but it's fixed in 5.4.0. See http://www.micro-optimization.com/global-function-vs-static-method

调用静态方法时曾经有过一个很大的惩罚 - 但它在5.4.0中得到修复。请参阅http://www.micro-optimization.com/global-function-vs-static-method

#5


1  

The difference about static and non static functions is a big answer and we have to look at Object Oriented Programming (OOP) in general.

静态和非静态函数的区别是一个很大的答案,我们必须总体上看一下面向对象编程(OOP)。

OOP in general, as you should know, is about Classes and Object, a class describe and creates objects based of it's description.

正如你所知,OOP通常是关于类和对象,类根据它的描述来描述和创建对象。

If you tell the class to contain the function "A" the object will have a callable function "A" who will have access to the objects parameters.

如果告诉类包含函数“A”,则对象将具有可调用函数“A”,该函数将具有对象参数的访问权限。

However if you tell the class to have a static function you tell the class to have a callable function WITHOUT instantiate an object.

但是,如果您告诉该类具有静态函数,则告诉该类具有可调用函数而不实例化对象。

Confusing? Can be at first and hard to understand why it's sometimes needed when you take your first OOP steps. Static functions is a good way to share info about classes without the need to make an object.

混乱?可以在最初并且很难理解为什么在您采取第一个OOP步骤时有时需要它。静态函数是一种分享有关类的信息而不需要创建对象的好方法。

An analgy would be: Take the class Car and the object BMW. the objects functions is about THE BMW, a static functions is about cars in general.

一个分析将是:乘坐汽车和物体宝马。对象的功能是关于THE BMW,静态功能一般是关于汽车的。

As of performance, it's not about performance at all, it's about design patterns. So performance-wise there is nothing to gain if the functions is static or non static.

在性能方面,它与性能无关,而与设计模式有关。因此,如果函数是静态的或非静态的,那么在性能方面没有任何好处。

#6


0  

From what the other guy said, if it uses more memory one way versus the other it will absolutely cause a performance difference. If you're using so much memory to the point of going into GC, it can cause queuing of processes due to backup, due to gc and take the whole system down potentially if it's an enterprise system with thousands of concurrent users. It may depend on the scale of the application.

从另一个人的说法来看,如果它使用更多的内存而不是另一个,它绝对会导致性能差异。如果您使用如此多的内存来进入GC,由于gc会导致进程因排队而导致排队,如果它是具有数千个并发用户的企业系统,则会导致整个系统崩溃。它可能取决于应用程序的规模。

We should never be of the opinion that a change in memory utilization is "minor". It's never minor in big systems...

我们永远不应该认为内存利用率的变化是“次要的”。在大型系统中它永远不会轻微......

#1


10  

1) What is difference between static function and normal function

1)静态功能和正常功能有什么区别

While they are functions, I'd prefer to call them methods of a given class. One is a static method and the other is an instance method.

虽然它们是函数,但我更喜欢称它们为给定类的方法。一个是静态方法,另一个是实例方法。

Static Method: $item = Item::getDetail(15);

静态方法:$ item = Item :: getDetail(15);

Instance Method: $item = getDetail(15);

实例方法:$ item = getDetail(15);

(refer to FuzzyTree's correct syntax above in the comments, however.)

(但是,请参阅上面评论中的FuzzyTree的正确语法。)

2) How to use static function and normal function (if you simple exemplify is good)

2)如何使用静态函数和正常函数(如果你简单的例子是好的)

Static means you do not have to instantiate (declare an object reference). That is, you can simply use the method. So, in your example, while the answer may be the same, the way you called that method/function is different, as you noted above.

静态意味着您不必实例化(声明对象引用)。也就是说,您可以简单地使用该方法。因此,在您的示例中,虽然答案可能相同,但您调用该方法/函数的方式也不同,如上所述。

In Java, for example, you have the Math class. It does not require instantiation to use, and in fact you cannot that I know of because its constructor is private. You can simply use a method by referencing the class and the method name you wish to use,

例如,在Java中,您拥有Math类。它不需要实例化使用,事实上你不能我所知道的,因为它的构造函数是私有的。您可以通过引用要使用的类和方法名称来简单地使用方法,

Math.pow(d1, d2);  //no instantiation needed

in PHP this might be,

在PHP中这可能是,

MyClass::pow(d1,d2); //no instantiation needed

Java: when to use static methods

Java:何时使用静态方法

3) Ask performance between static function and normal function. Which is better?

3)询问静态功能和正常功能之间的性能。哪个更好?

Better is a matter of your design. If you have to create an object every time you want to do the power of a number that will create more memoryusage than simply using the class directly. I do not have benchmark proof, but it seem logical since you are not handling the method the same way in memory. I do not think it will matter in real world unless you are doing a lot of complicated actions.

更好的是你的设计问题。如果你每次想要使用一个数字的力量来创建一个对象,那么就会产生更多的记忆,而不是简单地直接使用这个类。我没有基准证明,但它似乎合乎逻辑,因为你没有在内存中以相同的方式处理方法。除非你做了很多复杂的动作,否则我认为它在现实世界中并不重要。

Performance of static methods vs instance methods

静态方法与实例方法的性能

might also interest you.

也许你会感兴趣。

#2


2  

I'm assuming you're asking about the difference between a static method:

我假设你在询问静态方法之间的区别:

class Item {
    public static function getDetail($arg){}
}

And a function written outside of a class definition:

并且在类定义之外编写的函数:

function getDetail($arg){}

Static methods should be used over functions written outside of a class for organizational reasons. In an application with many files, having Item::getDetails($arg) will give a hint of where that function is defined. Also, having just a function written outside of a class runs the risk of name collision, if you start writing many functions outside classes.

出于组织原因,静态方法应该用于在类外部编写的函数。在具有许多文件的应用程序中,具有Item :: getDetails($ arg)将提示该函数的定义位置。此外,如果您开始在类之外编写许多函数,那么只有在类外部编写的函数会冒名称冲突的风险。

Especially if you are writing in the OOP style, you should be using static methods over functions outside of class definitions, but I think even in general, using static methods is the better way to go.

特别是如果你用OOP风格编写,你应该使用静态方法而不是类定义之外的函数,但我认为即使一般来说,使用静态方法也是更好的方法。

#3


1  

1.Entire difference is, you don't get $this supplied inside the static function. If you try to use $this, you'll get a Fatal error: Static functions are associated with the class, not an instance of the class and You will get an E_strict warning in the second case.

1.完全不同的是,你不会在静态函数中得到$ this。如果你试图使用$ this,你会得到致命错误:静态函数与类相关联,而不是类的实例,在第二种情况下你会收到E_strict警告。

2.Now static calling of non-static methods works but is deprecated. so use normal function.

2.现在静态调用非静态方法有效但不推荐使用。所以使用正常功能。

3.Technically speaking Static methods and variables are useful when you want to share information between objects of a class, or want to represent something that's related to the class itself, not any particular object. For some reason, PHP allows you to call non-static and static methods interchangeably,So performance wise or in whatever way you may say just use a normal function.Static is not going to help you at all

3.技术上讲静态方法和变量在你想要在类的对象之间共享信息,或者想要表示与类本身相关的东西而不是任何特定对象时非常有用。出于某种原因,PHP允许您交替地调用非静态和静态方法,因此性能明智或以任何方式您可以说只使用正常函数.Static根本不会帮助您

#4


1  

The difference between static and non static is that you can access a static method without having an instance of the class.

静态和非静态之间的区别在于您可以在没有类实例的情况下访问静态方法。

$item = getDetail(15); shouldn't work because detDetail is a method in a class and not a function.

$ item = getDetail(15);不应该工作因为detDetail是类中的方法而不是函数。

Performance of Static Methods

静态方法的性能

There used to be a big penalty when calling a static method - but it's fixed in 5.4.0. See http://www.micro-optimization.com/global-function-vs-static-method

调用静态方法时曾经有过一个很大的惩罚 - 但它在5.4.0中得到修复。请参阅http://www.micro-optimization.com/global-function-vs-static-method

#5


1  

The difference about static and non static functions is a big answer and we have to look at Object Oriented Programming (OOP) in general.

静态和非静态函数的区别是一个很大的答案,我们必须总体上看一下面向对象编程(OOP)。

OOP in general, as you should know, is about Classes and Object, a class describe and creates objects based of it's description.

正如你所知,OOP通常是关于类和对象,类根据它的描述来描述和创建对象。

If you tell the class to contain the function "A" the object will have a callable function "A" who will have access to the objects parameters.

如果告诉类包含函数“A”,则对象将具有可调用函数“A”,该函数将具有对象参数的访问权限。

However if you tell the class to have a static function you tell the class to have a callable function WITHOUT instantiate an object.

但是,如果您告诉该类具有静态函数,则告诉该类具有可调用函数而不实例化对象。

Confusing? Can be at first and hard to understand why it's sometimes needed when you take your first OOP steps. Static functions is a good way to share info about classes without the need to make an object.

混乱?可以在最初并且很难理解为什么在您采取第一个OOP步骤时有时需要它。静态函数是一种分享有关类的信息而不需要创建对象的好方法。

An analgy would be: Take the class Car and the object BMW. the objects functions is about THE BMW, a static functions is about cars in general.

一个分析将是:乘坐汽车和物体宝马。对象的功能是关于THE BMW,静态功能一般是关于汽车的。

As of performance, it's not about performance at all, it's about design patterns. So performance-wise there is nothing to gain if the functions is static or non static.

在性能方面,它与性能无关,而与设计模式有关。因此,如果函数是静态的或非静态的,那么在性能方面没有任何好处。

#6


0  

From what the other guy said, if it uses more memory one way versus the other it will absolutely cause a performance difference. If you're using so much memory to the point of going into GC, it can cause queuing of processes due to backup, due to gc and take the whole system down potentially if it's an enterprise system with thousands of concurrent users. It may depend on the scale of the application.

从另一个人的说法来看,如果它使用更多的内存而不是另一个,它绝对会导致性能差异。如果您使用如此多的内存来进入GC,由于gc会导致进程因排队而导致排队,如果它是具有数千个并发用户的企业系统,则会导致整个系统崩溃。它可能取决于应用程序的规模。

We should never be of the opinion that a change in memory utilization is "minor". It's never minor in big systems...

我们永远不应该认为内存利用率的变化是“次要的”。在大型系统中它永远不会轻微......