Learn from your fellow PHP developers with our PHP blogs, or help share the knowledge you've gained by writing your own.
$ composer require monolog/monolog
$ composer require monolog/monolog:1.18.0
$ composer require monolog/monolog:>1.18.0
$ composer require monolog/monolog:~1.18.0
$ composer require monolog/monolog:^1.18.0
$ composer global require "phpunit/phpunit:^5.3.*"
$ composer update
$ composer update monolog/monolog
4: Don’t install dev dependencies
In a lot of projects I am working on, I want to make sure that the libraries I download and install are working before I start working with them. To this end, many packages will include things like Unit Tests and documentation. This way I can run the unit Tests on my own to validate the package first. This is all fine and good, except when I don’t want them. There are times when I know the package well enough, or have used it enough, to not have to bother with any of that.5: Optimize your autoload
Regardless of whether you --prefer-dist or --prefer-source, when your package is incorporated into your project with require, it just adds it to the end of your autoloader. This isn’t always the best solution. Therefore Composer gives us the option to optimize the autoloader with the --optimize switch. Optimizing your autoloader converts your entire autoloader into classmaps. Instead of the autoloader having to use file_exists() to locate a file, Composer creates an array of file locations for each class. This can speed up your application by as much as 30%.$ composer dump-autoload --optimize
$ composer require monolog/monolog:~1.18.0 -o
composer require --dev phpunit/phpunit ^6.0
composer global require phpunit/phpunit ^6.0
~/.composer/vendor/bin
. If you add this directory to your path, then from any project, you can execute PHPUnit. However, as noted above, if you ever upgrade your globally installed packages then you will have problems.composer global update
composer require --dev
.wget https://phar.phpunit.de/phpunit-6.0.phar
chmod +x phpunit-6.0.phar
sudo mv phpunit-6.0.phar /usr/local/bin/phpunit
phpunit --version
Your biggest asset is also your biggest risk... your developers
composer require mongodb/mongodb
vendor
directory containing the required files.connect.php
. Add the following code:
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
$client = new Client("mongodb://localhost:27017");
?>
Client
class. We then create a new instance of the Client
class, specifying the MongoDB server's connection URL. Adjust the URL if your MongoDB server is running on a different host or port.insertOne()
method. Here's an example:<?php
$collection = $client->test->users;
$newUser = [
'name' => 'John',
'email' => '[email protected]',
'age' => 25
];
$insertResult = $collection->insertOne($newUser);
echo "Inserted document ID: " . $insertResult->getInsertedId();
?>
users
collection within the test
database. We create a new document as an associative array and then use the insertOne()
method to insert it into the collection. Finally, we retrieve and display the ID of the inserted document using the getInsertedId()
method.find()
method. Here's an example:<?php
$collection = $client->test->users;
$documents = $collection->find();
foreach ($documents as $document) {
echo $document['name'] . ': ' . $document['email'] . "\n";
}
?>
users
collection. We iterate over the result using a foreach
loop and access specific fields, such as the name
and email
, to display their values.updateOne()
method. Here's an example:<?php
$collection = $client->test->users;
$updateResult = $collection->updateOne(
['name' => 'John'],
['$set' => ['age' => 30]]
);
echo "Modified " . $updateResult->getModifiedCount() . " document(s).";
?>
age
field of the document with the name 'John' using the $set
operator. The updateOne()
method updates the first matching document. We then retrieve the number of modified documents using the getModifiedCount()
method.deleteOne()
method. Here's an example:<?php
$collection = $client->test->users;
$deleteResult = $collection->deleteOne(['name' => 'John']);
echo "Deleted " . $deleteResult->getDeletedCount() . " document(s).";
?>
deleteOne()
method removes the first matching document, and we retrieve the number of deleted documents using the getDeletedCount()
method.