New concepts introduced in Magento 2
In this article we will see some new concepts introduced in magento 2
Trait
Reflection
Example of reflection is as below:-
class A { public $property1; private $property2; public function method1() { echo "Method 1 of class A calling"; } } $reflector = new ReflectionClass('A'); $properties = $reflector->getProperties(); foreach($properties as $property) { echo $property->getName(); }
Note: Make sure Reflection library is installed on your server. See a more on reflection at http://php.net/manual/en/book.reflection.php
Reflection has huge number of method that will help us to do unit test of class. You can see the list of methods of interceptor here.
Interceptor
What was the problem with Magento 1 rewrite
Real case example of problem of Magento 1 rewrite:
Suppose an extension Cutehits_Checkout1 has extended onepage_checkout and overrided any specific method TestAction
Again Cutehits_Checkout2 has extended onepage_checkout and overrided same method (intentionally or unintentionally)
Here I want that to prioritize both, I mean Cutehits_Checkout1 should come in action first and then only Cutehits_Checkout2 should come in picuture. There is no proper way to handle this problem.
How Magento 2 handled the problem:
Magento 2 allows rewrite by creating seperate plugin for each. There are three type of interceptor plugin can be created
- Around
- Before
- After
And we can add sorting into it. See Interceptor in work by clicking here
With the help of interceptor we have improved it in various ways.
Dependency Injection
Magento2 Logging
- addDebug that will create log under var/log/system.log
- addInfo that will create log under var/log/exception.log
- addNotice that will create log under var/log/exception.log
- addError that will create log under var/log/exception.log
- critical that will create log under var/log/exception.log
See it in action
namespace Cutehits\First\Block; class First extends \Magento\Framework\View\Element\Template { protected $_logger; public function __construct( \Magento\Backend\Block\Template\Context $context, \Psr\Log\LoggerInterface $logger, array $data = [] ) { $this->_logger = $logger; parent::__construct($context, $data); } public function getHelloWorldTxt() { $message ="My logger message"; $this->_logger->debug('debug1234'); $this->_logger->addDebug($message); // log location: var/log/system.log $this->_logger->addInfo($message); // log location: var/log/exception.log $this->_logger->addNotice($message); // log location: var/log/exception.log $this->_logger->addError($message); // log location: var/log/exception.log $this->_logger->critical($message); // log location: var/log/exception.log return 'Hello world!'; } }
classMapGenerator
use Composer\Autoload\ClassMapGenerator; $classmap = ClassMapGenerator::createMap(__DIR__); print_r($classmap);
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments