


A newbie in PHP would like to ask: When should I use class and when should I use function?
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:
One-time mission?
Long-term maintenance, complex business logic, frequent changes?
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
