• 7
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

what is the difference between a class and a library?

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 on this. Thank you very much!

      • 1
    • i library, may have nothing to do with classes, it could just be a collection of procedural functions. a class has a well defined definition, a library, at least in php does not.

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.

  • 10
Reply Report

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.

  • 5
Reply Report

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?

  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions