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...
Join the discussion!
Comments
Want to comment on this lesson?
Enroll in this course to leave comments.