A week ago, I released the first version of Saeghe. I’m excited about it. Let me tell you what is Saeghe and how it can help you.
Attention needed
Saeghe is a difficult name to pronounce. Therefore, the Saeghe project has been renamed to phpkg.
Please visit phpkg website at phpkg.com
What is Saeghe?
In short, Saeghe is a modern PHP package manager. You may ask, why do we need a new package manager? To answer this question, we need to understand what a package manager is.
What is a package manager?
On Wikipedia: “A package manager or package-management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer in a consistent manner. A package manager deals with packages, distributions of software, and data in archive files.”
To simplify, it can help you to add any existing library from any git repository like GitHub or Gitlab to your project.
But in PHP, a package manager is more than this.
What is a PHP package manager?
If you have enough experience with PHP, you know how it works. In PHP, cloning a package and putting it in your project directory is not enough. You need to use the require or the include statements for each file in the package that you need to use in your project. One of the biggest annoyances in PHP was having to write a long list of needed includes at the beginning of each script. Many years ago, Composer has been born to solve this problem.
What is Composer?
Composer is a PHP package manager that autoloads required classes in your project. It uses PHP’s spl_autoload_register function to load required classes and solves the mentioned problem.
Using Composer has been a standard for many years in PHP. Any experienced PHP developer accordingly acquires Composer when they begin a project. It has been so natural to PHP developers that nobody can think of developing without it. But there is a problem with using Composer.
What is the problem with using Composer?
Composer uses PHP’s spl_autoload_register for autoloading classes. Now, let’s understand what is autoloading in PHP:
The spl_autoload_register() function registers any number of autoloaders, enabling classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.
Can you see the problem? Let me explain. As you can see in the PHP document, autoloading works with classes. By classes, I mean any class structures like interfaces, abstract classes, final classes, traits, enums, and lately, attributes. But you can not use autoloading for files that contain functions! You can add your files in composer.json to let Composer load them, but then you end up having the same problem that Composer meant to solve. You need to define each file separately. This time, instead of adding them in the PHP files using require or include, you have to define them in the composer.json file. Therefore this problem forced PHP developers to use classes for everything which introduces other problems.
What is the problem with using classes for everything?
Did you hear any of these?
- Do not use Singleton
- Try to use DTO
- Use invokable classes
- Use action classes
- Stop using extends
- Do not use abstract classes
- Use final classes
- Do not use inheritance
There are many others out there that can get added to this list. Some point out a problem and then suggest replacing it with a single-method class. Others approach a suggestion to how to create a class that is a single-method class. In most of them, there is no such thing as a state for the class!
In addition, you have seen a lot of classes that are nothing just a namespace for grouping some functions. Now when you have classes, you have the inheritance. But in some cases, it does not make sense to let the class get extended because it is doing a particular job, so you will see final classes get used as the solution to this problem instead of having a function.
You face many other problems when you try to use classes for everything.
The solution for these kinds of problems is to have namespaces contain functions. Now if you want to use functions, you have to add each one of them manually to the loader. This is the problem that Saeghe can solve for you.
How Saeghe solves the problem?
You can have classes and functions when you use Saeghe. Besides having the autoload functionality for autoloading your classes, it reads your code and adds the required includes wherever you need.
Also, PHP has great support for Functional Programming, by having namespaced functions, you can take advantage of it.
There are some other features that Saeghe adds to your project.
What can Saeghe do?
Besides package management, autoloading classes, and function compatibility, there are some other features that Saeghe can provide. Saeghe has a build process that reads your development code and builds them. Building in Saeghe means having a working copy of the project. You understand what I mean if you ever worked with any of the javascript package managers. By using Saeghe to build your project, it ends up having a copy of its files in your builds directory that required includes has added to them. Therefore you end up having decoupled environments for your development and production. This feature opens new doors to a bunch of cool stuff.
As a package manager, Saeghe does not need any intermediate repository (like packagist.org), you can pass the git URL directly to the add command and Saeghe will download the required files from the URL.
What now?
You can look at the official website and give it a shot. Supporting namespaced function files and also having separated environments opens new doors to many new possibilities. I hope you use it and participate in extending many new ideas in PHP.
You can also check this article to see how Saeghe works.