Create a singleton object with dependency injection in Magento
Magento dependency injection tutorial using singleton Session object in PHP 8 and PHP 7.
Lesson Content
We know how to create objects with Object Manager, and we also know that it is frowned upon in Magento land. Instead of using Object Manager, weโll create instances of objects with dependency injection instead.
This is easier than you think, especially since the introduction of promoted properties in PHP 8. Our first step is to create a constructor. In PHP, weโll do this by creating a public function named __construct:
Hereโs how the controller looks after adding an empty constructor:
<?php
declare(strict_types=1);
nam...
Join the discussion!
Comments
Want to comment on this lesson?
Enroll in this course to leave comments.