php 方法和函数有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4841605/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:32:10  来源:igfitidea点击:

What is a difference between a method and a function?

php

提问by Amol

What is the difference between a method and a function? Is it that a method returns a value and a function doesn't?

方法和函数有什么区别?是方法返回值而函数没有返回值吗?

回答by Sarfraz

Method is actually a functionused in the contextof a class/object.

方法实际上是在类/对象的上下文中使用的函数

When you create a function outside of a class/object, you can call it a functionbut when you create a function inside a class, you can call it a method.

在类/对象之外创建函数时,可以将其称为函数,但在类内部创建函数时,可以将其称为方法

class foo {
   public function bar() { // a method
     ........
   }
}


function bar() {  // a function not part of an object
}


So an object can have methods (functions) and properties (variables).

所以一个对象可以有方法(函数)和属性(变量)。

回答by Jonatan

The words are not opposed to each other but rather describes two possible aspects of a subroutine. An attempt to define the words follows:

这些词并不相互对立,而是描述了子程序的两个可能方面。试图定义这些词如下:

Subroutine:A set of instructions that can be used several times in the same program.

子程序:一组可以在同一个程序中多次使用的指令。

Function:A subroutine that returns a value. Derived from functions in mathematics (wikipedia).

功能:一个返回值的子程序。源自数学中的函数(维基百科)。

Method:A subroutine that belongs to an object or a class. Could be a function.

方法:属于对象或类的子程序。可能是一个函数。

I tend to use the word "function" for every subroutine that has no side effects but returns one clear value and the word "method" for every subroutine that has a side effect.

我倾向于对每个没有副作用但返回一个明确值的子程序使用“函数”一词,对每个有副作用的子程序使用“方法”一词。

回答by origo

The difference between the expressions "method" and "function" is that a "method" is a member function of a class, whereas a standalone function does not, and a standalone function usually exists in global context.

表达“方法”和“函数”之间的区别在于,“方法”是类的成员函数,而独立函数不是,并且独立函数通常存在于全局上下文中。

回答by Furqan Hameedi

Both are used interchangeably, but function is the terminology used in structural languages and method is the terminology used in Object Oriented Langauages. Also methods exists within objects while functions can exist without objects as well.

两者可以互换使用,但函数是结构语言中使用的术语,方法是面向对象语言中使用的术语。方法也存在于对象中,而函数也可以在没有对象的情况下存在。

回答by Milan Saha

Function is a generic term to be used in procedural programming approach where as Method is a term to be used in Object oriented programming approach to define a class property.

函数是在过程编程方法中使用的通用术语,而方法是在面向对象的编程方法中用于定义类属性的术语。

回答by J.Rob

We define method inside class , we define function out side class, function is not part of class

我们在类内部定义方法,我们在类外部定义函数,函数不是类的一部分

回答by Piyush Mattoo

In one line, a method is a function but a function is not necessarily a method. The difference is that a method is used to describe functions defined in classes that are used with instances of those classes.

一方面,方法是函数,但函数不一定是方法。不同之处在于,方法用于描述在与这些类​​的实例一起使用的类中定义的函数。

package {class Example {
  public function iAmAMethod():void {
     addEventListener("listenerFunctionIsNotAMethod", function(event:Event):void {
        trace("inline function, yay!");
     });
  }

}

}