Home > Backend Development > PHP Tutorial > A newbie in PHP would like to ask: When should I use class and when should I use function?

A newbie in PHP would like to ask: When should I use class and when should I use function?

WBOY
Release: 2016-09-19 09:16:33
Original
1801 people have browsed it

Some problems can be solved by classes or directly using functions. And I see that in some PHP frameworks, the methods in many classes do not need to rely on instances of this class. These methods are instrumental methods, and there are no attributes (variables) in these classes. In this case, it is still better to use classes. What's the point? If you don't declare static, you have to instantiate it before you can call it, which is much more troublesome than a function. So why do many PHP class libraries use a bunch of classes to implement functions that can be achieved with just a few functions?

So, under what circumstances should function be used, and under what circumstances should class be used?

Reply content:

Some problems can be solved by classes or directly using functions. And I see that in some PHP frameworks, the methods in many classes do not need to rely on instances of this class. These methods are instrumental methods, and there are no attributes (variables) in these classes. In this case, it is still better to use classes. What's the point? If you don't declare static, you have to instantiate it before you can call it, which is much more troublesome than a function. So why do many PHP class libraries use a bunch of classes to implement functions that can be achieved with just a few functions?

So, under what circumstances should function be used, and under what circumstances should class be used?

The function of class is to package functions. When you need to package multiple functions into a module (class), use class instead of bare function.

When to use it? It’s a matter of personal opinion and local conditions. There is no need to pursue a perfect answer

I think the questioner may not have a thorough understanding of object-oriented philosophy.
Object-oriented class is not as simple as encapsulating a bunch of functions into a module. It allows you to abstract various things in the code world into a thing (that is, an object), and then give it actions and labels. That is, methods and properties.
For example, if the user member can log in and post, and his name is Chang Wei, then you can give him the login and posting methods, as well as a name attribute.
So my answer to your question is, if it is a very small function or a very general function that is not coupled to any object, use function, otherwise use class.

This is a very interesting question, as you said: "Under what circumstances should function be used, and under what circumstances should class be used?"

What’s going on? Just give 3 examples:

  1. One-time mission?

  2. Long-term maintenance, complex business logic, frequent changes?

  3. Long-term maintenance, basic support, such as development framework?

Usually for one-time tasks, it doesn’t matter what you use, just complete the task. Tasks that require long-term maintenance are not just functions or classes. This is an engineering, software engineering.

Compared with the past, enterprise-level software systems are larger and more complex, and change more frequently. How to reduce complexity and cope with changes has promoted software engineering research and has also given rise to the evolution of high-level languages ​​with stronger expressive capabilities. In terms of design, we use decomposition, abstraction, hierarchical structure and other means to reduce complexity and cope with changes.

The blind prescription given by object-oriented technology includes principles such as abstraction, encapsulation, modularity, hierarchy, type, concurrency, and persistence. There are many traditional medicines handed down from ancient times. It has also left many classics such as "Programming Style", "Code Encyclopedia", "Agile Software Development-Principles, Patterns and Practices", "The Art of Writing Readable Code", "Software Design Refactoring", "Enterprise Application Architecture Patterns" 》......

So what’s the answer? I don’t know either. Maybe you need to torture yourself with a complex system to understand some principles to guide practice and integrate knowledge with action.

If there are many tool functions, it will be clearer to call them as static functions in the tool class, and it will also be convenient to classify tool functions for different purposes.

Categories.

<code>class User

function name
function sex
function age
...</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template