New concepts introduced in Magento 2

In this article we will see some new concepts introduced in magento 2

Trait

Trait is a new feature introduced in PHP 5.4 that has provided a new way to use multiple inheritence. In other words trait has overcome the problem of interface. See details of traits at here

Reflection

Reflection is a PHP library that provides the various method which can be used to get some information (like what are all methods, properties that class has, what is the class type/is it a subclass/ what is the return type of any specific method etc..). Reflection can be used majorly for code coverage and unit testing implementation for features.

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

Interceptor is a pattern that is being used by Magento 2.0 to rewrite the feature. There were a lot of issues we were facing with magento 1.x rewrite.

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

Dependency Injection is a another development pattern that is being used by magento 2. Earlier in Magento 1 factory design pattern were in use for initialization of class. It was giving an option to initialize the class by another class. See how classes were getting instantiated in Magento 1 and how Dependency Injection has resolved the problem. I have explained the problem by using factory pattern. Dependency Injection has fixed this problem magically.


Magento2 Logging

Magento uses ‘\Psr\Log\LoggerInterface’ class. There are four types of logging in available in Magento.

  • 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

classMapGenerator is a concept of Symfony that is a classLoader component that automatically load the class. It gives the flexiblity to write class name directly that will automatically be mapped to their respective file path.For example class name Acme\Bar\Baz will automatilly mapped with library/bar/baz/Boo.php

use Composer\Autoload\ClassMapGenerator;

$classmap = ClassMapGenerator::createMap(__DIR__);

print_r($classmap);

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...