Home >Backend Development >PHP Problem >What is the difference between methods and functions in php
The difference between methods and functions in php is: methods depend on the existence of the class, are defined in object-oriented, and can only be called through objects (static methods of a class can be called directly through the class name); functions are It exists alone and is defined in a process-oriented manner, with the purpose of solving a general class of problems.
In PHP, a method is also a type of function. It generally represents a "function" in a class and can only be called through an object (static methods of a class can be class name directly).
(Recommended tutorial: php tutorial)
The functions we usually talk about should be functions in the global scope. They usually exist alone and are intended to solve a common problem. After being introduced, it can be called directly anywhere in the program.
Detailed introduction:
The function exists alone, that is, it is defined in the process-oriented part.
The method depends on the existence of the class, which is defined in object-oriented.
We can regard functions as the implementation of an algorithm and methods as the implementation of business logic.
Class methods can set access permissions and need to be called through objects or classes; functions are public and can be used.
The above is the detailed content of What is the difference between methods and functions in php. For more information, please follow other related articles on the PHP Chinese website!