Php beginners ask: when should class be used and function be used?

Source: Internet
Author: User
In some cases, classes and functions can be used directly. In addition, in some php frameworks, methods in many classes do not need to depend on instances of this class. these methods are tool-based methods and do not have any attributes (variables) in these classes ), in this case, what is the significance of using the class... in some cases, classes and functions can be used directly. In addition, in some php frameworks, methods in many classes do not need to depend on instances of this class. these methods are tool-based methods and do not have any attributes (variables) in these classes ), in this case, what is the significance of using classes? If the static statement is not declared, it must be instantiated before calling. this is much more troublesome than the function. So why do many php class libraries use a bunch of classes to implement several functions?

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

Reply content:

In some cases, classes and functions can be used directly. In addition, in some php frameworks, methods in many classes do not need to depend on instances of this class. these methods are tool-based methods and do not have any attributes (variables) in these classes ), in this case, what is the significance of using classes? If the static statement is not declared, it must be instantiated before calling. this is much more troublesome than the function. So why do many php class libraries use a bunch of classes to implement several functions?

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

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

When to use it, be benevolent, and adapt to local conditions, without the need to pursue a perfect answer

I think the subject may not have a thorough understanding of the object-oriented philosophy.
The object-oriented class is not as simple as encapsulating a bunch of functions into a module. It also allows you to abstract various things in the code world into one thing (that is, an object ), then, assign his actions and tags, that is, methods and attributes.
For example, if you use member, you can log on to Memcached and post a message. His name is Changwei. you can also use the name attribute to log on to Memcached and post a message.
So your answer to this question is: if there are some very small functions or very common functions that do not have any coupling relationship with the object, use the function; otherwise, use the 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 is the situation? For example:

  1. One-time task?

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

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

Generally, you can complete a one-time task. Tasks that require long-term maintenance are not just about functions or classes. This is an engineering, software engineering.

Compared with the past, enterprise-level software systems are larger and more complex, with more frequent changes. how to reduce complexity and respond to changes has promoted software engineering research, it also gave birth to the evolution of advanced languages with higher expressive power. In terms of design, we can reduce complexity and respond to changes by means of decomposition, abstraction, and hierarchy.

The blind prescription provided by object-oriented technology, including abstraction, encapsulation, modularity, hierarchy, type, concurrency, durability, and other principles. There are still a lot of traditional prescriptions. It also leaves many excellent books such as "programming style", "code Daquan", "Agile software development-principles, models and practices", "art of writing readable code", and "software design reconstruction"., Enterprise Application Architecture model...

So what is the answer? I don't know, maybe you need a complicated system to torture, understand some principles to guide practices, and combine knowledge and practice.

If there are many tool functions, it is clearer to call the functions as static functions in the tool class, and the tool functions for different purposes can be easily classified.

Category.

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.