PHP Novice ask: When should use class, when should use function?

Source: Internet
Author: User
On some issues, classes and direct functions can be solved. 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 tool-based methods, and there are no attributes (variables) in these classes, so what is the use of the class? If you do not declare static, you have to instantiate it before it can be called, which is more cumbersome than the function. So why do many PHP libraries have to use a bunch of classes to implement functions that can be implemented in a few function?

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

Reply content:

On some issues, classes and direct functions can be solved. 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 tool-based methods, and there are no attributes (variables) in these classes, so what is the use of the class? If you do not declare static, you have to instantiate it before it can be called, which is more cumbersome than the function. So why do many PHP libraries have to use a bunch of classes to implement functions that can be implemented in a few function?

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

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

When to use, a matter of opinion, local conditions, do not have to pursue the perfect answer

I think the Lord may have a clear understanding of the object-oriented philosophy.
The object-oriented class is not to encapsulate a bunch of function into a module so simple, he is more let you abstract the code world of Things into a thing (that is, object), and then give him actions and tags, that is, methods and properties.
For example user member He can login, can post, his name is Changwei, then can give him landing and posting method, there is a name attribute.
So your question my answer is, if it is a small function, or a very general function, and does not have a coupling relationship with any object in the case of function, otherwise it is using class.

This is a very interesting question, as you said: "In what circumstances should use function, under what circumstances should use class?"

What's the situation? To give 3 cases randomly:

    1. A one-time mission?

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

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

Usually one-time tasks, with what does not matter, to complete the task can. Tasks that require long-term maintenance are more than just function or class issues. This is a project, software engineering.

Enterprise software systems are larger and more complex and more frequent than in the past, how to reduce complexity, respond to change, drive software engineering research, and lead to the evolution of higher-level languages with stronger expressive power. In the design, through decomposition, abstraction, hierarchical structure and other means to reduce complexity, to deal with change.

The prescription of the object-oriented technology includes abstraction, encapsulation, modularization, hierarchy, type, concurrency, persistence and other principles. There are a lot of prescriptions handed down. Also left many classics such as "Programming Style", "Code Encyclopedia", "Agile software Development-principles, patterns and practices", "writing the Art of readable Code", "Software Design Refactoring", "Enterprise Application architecture Model" ...

So what's the answer? I don't know, maybe you need a complicated system to torture, to understand some principles to guide practice, to know the line of unity.

If the function of the tool is more, the package in the tool class as a static function to call will be more clear, but also convenient for different uses of the tool function classification.

Classified.

class Userfunction namefunction sexfunction age...
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.