开发者

what is the difference between a class and a library?

开发者 https://www.devze.com 2023-02-10 03:32 出处:网络
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. Sorry for my limited knowledge

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. Sorry for my limited knowledge for this, but I would appreciate it if you could enlighten me a little bit o开发者_JAVA百科n this. Thank you very much!


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号