Video Lesson: Create a singleton object with dependency injection in Magento

Create a singleton object with dependency injection in Magento

This is premium content

Only available to enrolled or University students.
Join today to unlock all lessons in this course.

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...

Premium content

Enroll to unlock the full content and all course materials.

Maggie

Hey, I'm Maggie! ๐Ÿ‘‹

Ask me anything.

Want to chat with Maggie?

Enroll in this course for AI tutor access.

Comments

Join the discussion!

Comments

Want to comment on this lesson?

Enroll in this course to leave comments.