what is the difference between a class and a library?

后端 未结 3 924
野的像风
野的像风 2020-12-31 15:09

I googled, and was informed that a library is made up of multiple relevant classes. But in Codeigniter, I found that there is virtually only one Class in every Library. Sorr

相关标签:
3条回答
  • 2020-12-31 15:27

    In general programming terms, not every library has to be made of multiple classes. In fact, not all libraries have to be made up of classes either — it really depends on the implementation (and sometimes the language). As Wikipedia starts off (italics mine):

    In computer science, a library is a collection of resources used to develop software. These may include subroutines, classes, values or type specifications.

    CodeIgniter has its own definition of "library", in this case it simply calls each third-party application class its own library. Despite that, you can include other classes in the same library file, as long as you have at least one class with the same name as the library file.

    0 讨论(0)
  • 2020-12-31 15:35

    The difference is a semantic one.

    A Class is an implementation of a specific piece of functionality (usually completely encapsulating the functionality.

    A Library is a collection of units of functionality (or just one) that add functionality. Notice I tried to stay away from the word class in that definition. Libraries can be procedural, functional or OOP. That doesn't detract from the fact that it's a library. Classes just help the abstraction when dealing with OOP.

    A Framework is a library that imparts architecture choices on how you write code.

    Every framework is therefore a library. Not every library is a framework. CodeIgniter itself can be used as a framework or a library. The difference is if you let the libraries direct your architecture, you're using a framework. If you do not use the architecture bit, it's a library.

    It's definitely a pedantic difference, but a significant one. As a gross-over-simplification, if you're doing a formal architecture and understand why everything is layered out how it is for your application specifically, you're using it as a library. If instead you're building it that way because that's how CI does it, that's using a framework. Both have significant benfits, but it's worth understanding the difference.

    0 讨论(0)
  • 2020-12-31 15:43

    Checking the CodeIgniter docs:

    When we use the term "Libraries" we are normally referring to the classes that are located in the libraries directory and described in the Class Reference of this user guide. In this case, however, we will instead describe how you can create your own libraries within your application/libraries directory in order to maintain separation between your local resources and the global framework resources.

    I don't think of a "library" as specific to a fixed number of classes or files. Does this quote refer to the specific usage that has you confused?

    0 讨论(0)
提交回复
热议问题