Amol Amol - 4 months ago 9
PHP Question

What is a difference between a method and a function?

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

Answer

Method is actually a function used in the context of a class/object.

When you create a function outside of a class/object, you can call it a function but 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).

Comments