Dependency Management Intro

A cornerstone of modern software development practices

We’ve made it to almost 100 articles without discussing dependencies in PHP. We ended the previous article on the DateTime built in class. We used it to perform some date and time operations. Next on the list is Carbon. Carbon is a dependency that’s installable using composer.

Once installed, we can use it like any other built in class. Carbon actually inherits from the DateTime class and expands on it. But, before we get into that, I wanted to briefly touch on dependencies in PHP.

You might have heard on npm if you’ve ever worked on any front-end application that uses JavaScript. npm is a dependency manager. The npm registry contains hundreds of thousands of dependencies. These are pieces of code that you can download and use in your application. Someone else maintains them, most of the time, and you just reap the benefits of someone else’s hard work.

PHP has a dependency manager too. It’s called composer. Any time that you need to include a library for your use, you can use composer. In our case, we’ll need to use it for the Carbon dependency.

To get composer, you’ll need to visit the composer url and download it. You can do this in your docker environment too (which is preferred).

https://getcomposer.org/?source=post_page—–fe42877964d4——————————–

Once you install composer on your operating system, you can run composer commands to grab the dependencies that you need.

For example, composer require nesbot/carbon grabs the Carbon dependency. This automatically grabs the latest carbon version and even configures the composer.json file for you.

{
    "require": {
        "nesbot/carbon": "^2.63"
    }
}

If you look at your folder structure, you’ll notice that you now have a vendor directory and within it composer and any other dependencies that composer requires.

There’s also an autoload.php which contains the autoloader. This allows you to skip all of the require statements and instead just loads everything for you that you need. You will need to require it however.

We can create a new file, require our autoloader, use our Carbon\Carbon class, since it’s in a virtual directory (the Carbon namespace), and start using it.

<?php
require 'vendor/autoload.php';

use Carbon\Carbon;

echo Carbon::now();

We get a simple result: 2022–11–24 03:26:39.

And that’s how easy dependencies can be. If you need to update your dependencies, use composer update. We won’t get deeper than this, since this is an introduction to dependencies. In the next article, we’ll look at Carbon in more detail.

https://github.com/dinocajic/php-youtube-tutorials?source=post_page—–fe42877964d4——————————–

DateTime Object

THE DATETIME OBJECT IN PHP SIMPLIFIES DATE AND TIME MANIPULATION

PHP – P98: DATETIME OBJECT

How do you add 2 weeks to a current date-time or how many days are between two dates? That’s where the DateTime object shines.

Dependency Management Intro

A cornerstone of modern software development practices

PHP – P99: dependency management

Dependency management ensures smooth integration of software components. It’s essential for maintaining software stability and updates.

Carbon

POPULAR PHP LIBRARY FOR WORKING WITH DATES AND TIMES

PHP – P100: carbon

Carbon is a PHP dependency that extends the DateTime class. It has a lot more simplified interface and is extremely simple to use.

Leave a Reply