0

I know in Magento 2.x versions the namespace is converted to actual file path to load classes runtime.

For example, \Magento\Catalog\Model\Product is converted to path\to\webroot\magentorootdir\vendor\magento\module-catalog\Model\Product.php, but how this conversion done ? Is there any built-in function for this?, if yes, then where is it ? Can that function be called in a dirty playground script ?

1

Magento's autoload functionionality can be found in /vendor/composer/ClassLoader.php. Here is where the autoload handler gets registered:

public function register($prepend = false)
{
    spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}

This class is instantiated by /vendor/composer/autoload_real.php's getLoader method. This is also where the various maps get build using the autoload_*.php files, for example autoload_static.php builds the map for Magento's modules, for example:

'Magento\\Catalog\\' => 
array (
    0 => __DIR__ . '/..' . '/magento/module-catalog',
),

So when a class such as Magento\Catalog\Model\Product needs to be instantiated, a call is made to ClassLoader->loadClass('Magento\Catalog\Model\Product') which in turn calls $this->findFile(Magento\Catalog\Model\Product) to try and find a matching file name based on ClassLoader's mappings and then includes the file if it exists.

All of this happens as a result of app/autoload.php being included, so I guess that's what your 'dirty playground' script would need to do.

|improve this answer|||||
0

Actually its based on the Framework Magento uses.

Basically you define the Namespaces of each modules which then get pumped into an huge iterator that holds ALL methods which are registered within such modules.

From your example you might wanna utilize \Magento\Catalog\Model\Product. The file descriptor is not needed since the classname is the same as the filename. If you check product.php you will see it yourself.

<?php

namespace Magento\Catalog\Model;

class Category extends \Magento\Catalog\Model\AbstractModel ...
{

So if you are trying to debugging some outputs you might wanna enable Block Name output. From there on you can further debug.

Dont get confused if you see Folders and files with the very same name. Differences are, that the folders doesn't contain any methods, but further .php-files and the file itself contains an actual class.

Example:

\Magento\Catalog\Model\Category = vendor/magento/module-catalog/Model/Category.php

\Magento\Catalog\Model\Category\Attribute = vendor/magento/module-catalog/Model/Category/Attribute.php

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.