All posts by info@phpcodez.com

Return type declarations

Whereas type hints ensure input consistency, return type declarations ensure output consistency.

We use a colon before the opening curly brace of a function to hint the return type.

The same strictness rules apply as with the type hints: if “strict mode” is disabled, return values that can be converted to the preferred type are allowed. If you enable “strict mode” this code will throw a type error.

Type hints

Type hints have been available in PHP for while now. Unfortunately they were restricted to classes, arrays and callables.

As of PHP 7, the scalar types (integers, floating point numbers, booleans and strings) can also be used as type hints.

it allows developers to ensure a better input consistency of a function/method interface. By default “coercive mode” is enabled. This restricts PHP from throwing a type error when the types don’t exactly match, but when a conversion is still possible.

If you enable “strict mode” , a type error is thrown when the signatures don’t match.

PHP 7 New Features

  • Scalar type hints
  • Return type declarations
  • Anonymous classes
  • The Closure::call() method
  • Generator delegation
  • Generator return expressions
  • The null coalesce operator
  • The space ship operator
  • Throwables
  • Level support for the dirname() function
  • The Integer division function
  • Uniform variable syntax

Extension Attributes

Extension attributes are new in Magento 2. They are used to extend functionalities and often use more complex data types than custom attributes. Extension Attributes are used to allow for customization of the strict Service Contracts. These attributes do not appear on the GUI.

Third-party developers cannot change the API Data interfaces defined in the Magento Core code. However, most of these entities have a feature called extension attributes. Check the interface for the methods getExtensionAttributes() and setExtensionAttributes() to determine if they are available for the entity.

Public interfaces

A public interface is a set of code that third-party developers can call, implement, or build as a plug-in. Magento guarantees that this code will not change in subsequent releases without a major version change.

Public interfaces for a module are marked with @api annotation.

Service contracts

Magento is a modular system that enables third-party developers to customize and overwrite core parts of its framework. This flexibility, however, comes at a price. Business logic tends to leak across the layers of the Magento system, which manifests as duplicated and inconsistent code.

Merchants might be reluctant to upgrade Magento because customized extensions that they have purchased might not be compatible with new versions of Magento. Also, Magento and third-party developers can find it difficult to track and report the dependencies that customized extensions have on other extensions.

To address these issues, the Magento system introduces service contracts.

A service contract is a set of PHP interfaces that are defined for a module. A service contract includes data interfaces, which preserve data integrity, and service interfaces, which hide business logic details from service requestors such as controllers, web services, and other modules.

If developers define data and service interfaces according to a set of design patterns, the result is a well-defined, durable API that other modules and third-party extensions can implement through Magento models and resource models.

Magento 2.3: Error Assigning Related Products

check the following tables in your DB:

catalog_product_link_attribute and catalog_product_link_type

If those are empty, you need to run the following SQL queries:

INSERT INTO catalog_product_link_attribute (product_link_attribute_id, link_type_id, product_link_attribute_code, data_type) VALUES
(1, 1, ‘position’, ‘int’),
(2, 4, ‘position’, ‘int’),
(3, 5, ‘position’, ‘int’),
(4, 3, ‘position’, ‘int’),
(5, 3, ‘qty’, ‘decimal’);

INSERT INTO catalog_product_link_type (link_type_id, code) VALUES
(1, ‘relation’),
(3, ‘super’),
(4, ‘up_sell’),
(5, ‘cross_sell’);

Cluster

A cluster is the group of computing machines that can individually run the software. Clusters are typically utilized to achieve high availability for server software.

Clustering is used in many types of servers for high availability.

App Server Cluster – An app server cluster is the group of machines that can run an application server that can be reliably utilized with a minimum of downtime.

Database Server Cluster – A database server cluster is the group of machines that can run a database server that can be reliably utilized with a minimum of downtime.