Video Lesson: Incorrect way to create objects with new keyword in Magento

Incorrect way to create objects with new keyword in Magento

This is premium content

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

Incorrect way to create objects with new keyword in Magento

Learn why the new keyword cannot create Magento objects and how dependency injection handles required class dependencies.

Lesson Content

We just learned why we shouldn’t use the new keyword in Magento, but let’s walk through an example to learn why.

We are currently just returning a die() function call in our controller’s execute() method, but that’s obviously not what we want to do. We want to instead return a regular Magento page response.

Creating an object with PHP is really easy. You just specify a class using the new keyword. We can see an example of this by using the new keyword to create a Session object. This Session class will be pulled from the Magento\Customer\Model.

Let’s output this to the...

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.