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);

Related posts:

The following two tabs change content below.

Chandra Shekhar

Consultant at Infogain
Chandra Shekhar is a LAMP Developer,Thinker,Blogger and currently working as Magento(Ecommerce) specialist.. I am always open to learn new things in LAMP as well as other newer technology. In the journey of my last 9 years of web and product development, I have decided to share my knowledge and experience to Open Source Community in 2011. In this regard I have started CuteHits.com. Since then I am actively sharing my knowledge on various LAMP technology through CuteHits.com’s technical ... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)