Lesson 1 - Introduction to libraries and frameworks for PHP

PHP Libraries Introduction to libraries and frameworks for PHP

Welcome to the first lesson of our online course about creating libraries for PHP. During the course, we'll create several very useful libraries for various purposes, step by step. We'll create PHP libraries for things such as date and time, image manipulation, strings, and forms.

For who is this course

This course will be a bit different from the previous ones, here, at ICT.social. It will help you improve your workflow quite a lot, however, it'll be written in a relatively advanced manner.

I'm sure it goes without saying, but this course requires that you are already familiar with object-oriented programming.

Reasons to use PHP libraries

In PHP, one simply cannot create a serious project without using high quality libraries. This is due to two reasons, specifically:

  1. Although, PHP is very high-level language and comes with plenty of useful functions, eventually, you'll encounter weaknesses and gaps in its standard libraries. Many important functions are missing or are hard to work with. Once you notice that you're constantly programming the same thing such as form handling or date formatting over and over again in every project, you should consider creating a few simple libraries for these common project parts.
  2. With all of my years of experience, I can confidently tell you that if you program in plain PHP, about 50% of all of your code will be redundant garbage. You could write your application in half the time and with half the amount of code using quality libraries. The application will be easy to maintain and you'll be able to enjoy developing it. This way, you won't have to reinvent the wheel and constantly encounter the problems (like determining whether a user is an administrator). Instead, all you'll have to do is work on the functionality for that particular application, which is what programming is really about.

Library vs. framework

You may have previously encountered the term framework, which is closely related to libraries. A framework is, simply speaking, a set of libraries which are made to work together to create one large library, providing complex functionality. They're usually made by a single author or by a group of authors.

Framework advantages

With this course, you'll get a functional set of libraries which you can use immediately on a variety of projects. All of this routinely sorts of work such as signing users in or form handling will be taken care of and ready to use. All we'll have to do is compose our application using said libraries as power tools of sorts and then adding our custom application logic. Sounds great, doesn't it?

When to use a framework

The answer to the question "Should I use/make a framework?" is: "Sure, just not before you can program very well.". Frameworks are advanced applications, and even if you were to use some sort of third-party framework, you'd have to be a good programmer to use them properly. This applies even if the framework's author guarantees that their framework can be used practically without any background information.

Building a custom framework

Creating a custom framework is not as hard as it seems. Understanding the MVC architecture is pretty much the only advanced knowledge required to create several different kinds of libraries. Aside from all of that, one can learn quite a lot in doing so, creating libraries, which is exactly why we made this course. We'll create the most crucial libraries for common situations. With these libraries, alongside the MVC architecture course, we'll reach programmer nirvana. We'll create a tool which will enable us to enjoy our everyday work and will spare us a lot of time.

Another option is to pick up a pre-made third-party framework. There are several PHP frameworks out there. If you do so, you'll immediately get high-quality functionality without having to break a sweat and for free. Either way, you should consider the following disadvantages of that decision.

Disadvantages of third-party frameworks

- Development is not guaranteed - You must understand that a particular framework is not a standard part of PHP, it's third-party software. No one can guarantee that it's right, that it works properly, that it will be developed for future versions of PHP and or that its interface won't change too often. If you choose the wrong one and build your application using a low-quality framework, you may end up throwing it away and starting over. The same sort of thing happens with smaller frameworks. Although they're high-quality, their authors often decide to terminate their development.
- Interfaces constantly change - Every framework has their own specific interface. You have to learn it well in order to be able to use the framework. When a framework is developed, the interface often changes.
- Naive feeling of safety - You may end up creating as many security leaks using a framework wrong as with not using one at all, it's still simply PHP.

To be fair, let's go over a few disadvantages to using a custom framework.

Disadvantages of custom framework

- Potentially incorrect design - Especially if you're still learning to program, it's likely that you'll add flawed design structures into your libraries. Also, if you build your application and include said mistake(s), it's usually hard to go back and fix them (mainly if it's a large-scale application).
- Security - You have to be familiar with at least the most basic/common security issues and know which PHP functions you won't normally encounter. I personally see this point as an advantage, since this sort of knowledge is extremely valuable. However, not everyone has the time and patience for that.

  • A lot of work - Although, a custom framework will save you time in the future, there's usually quite a lot of code that has to be written and can delay the project's startup.

The final solution

Every good programmer should try to implement a simple MVC framework and several libraries at least once in order to understand the basic concepts and principles. It's hard to learn a principle based on a black-box monolith. It's also dangerous if you don't know how it works internally because you may end up with low-quality performance, security, overall design, and so on... Once you understand the key principles, it's up to you to decide whether to improve your simple framework or to use that knowledge to switch to an outstanding third party framework. Preferably, one that has been developed intensively for multiple years, e.g. Symfony.

Regardless of whether you build applications upon your own framework or a third-party framework, your decision will always have some particular advantages and disadvantages. Neither of these decisions is completely right or completely wrong.

For people who make many different and relatively small websites, the third-party framework is an ideal solution. For those who develop custom projects or a few projects used for multiple years, a custom framework could benefit you since no one will change the interface. You wouldn't have to dread over development changes in the framework, which is important in large-scale applications.

That would be all for the introduction. Now you know what this course is all about and why it's important to do so. In the next lesson, DateUtils library for formatting date and time in PHP, we'll create our first PHP library. It'll be a simple, yet surprisingly useful utility class which formats date and time to your regional format.


 

 

Article has been written for you by David Capka
Avatar
Do you like this article?
No one has rated this quite yet, be the first one!
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn College The author learned IT at the Unicorn College - a prestigious college providing education on IT and economics.
Activities (4)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!