![]() Testing it on the command line with php index.php should produce the desired result. Inside this file, we are going to have the following code: convertToCm ( 1 ) echo $result $result = $conv -> convertToInch ( 1 ) echo $result Īs you can see, we are using the require_once '/vendor/autoload.php' line to autoload our class and then using it to do some conversions. Let’s create a /src/Converter/Converter.php file. We are going to create a simple class that converts inches to centimetres and centimetres to inches. Let’s create a simple PHP package so we can see how easy it is to submit it to Packagist. Not only does Packagist aggregate PHP packages, it also encourages PHP developers to submit their own packages, strengthening the community. This way, PHP developers don’t have to spend days searching for packages on Github. The Packagist website also has information on all the packages and a useful search and sort engine. This is where all the Composer packages are aggregated, which means that when we execute composer search we are actually searching Packagist. Check out the documentation for more information on autoloading with Composer. In addition to PSR-4 autoloading, Composer also supports PSR-0, classmap, and files autoloading. This way we are adding our own code to the autoloader. In dump-autoload‘s case, there’s no other command in the Composer namespace that starts with du, so Composer knows that’s the one we want. Pro-tip: composer du would also work, because composer runs any command which is uniquely identified by its first few letters. Composer provides a command for this: composer dump-autoload. After this, we need to re-generate the vendor/autoload.php file. The /src folder should be at the same level as the /vendor folder (root of the project). Then, we map the Foo namespace to the /src folder. Here’s an example:įirst, Composer will register a PSR-4 autoloader for the Foo namespace. Manually write the composer.json file (not recommended – easy to slip up with syntax and brings no apparent benefits).This command will not only create the composer.json file, it will also download all the dependencies specified. After we choose the package we want, we can then use composer require. ![]()
0 Comments
Leave a Reply. |