Ques
Ques

Reputation: 265

Autoload function typo3 7.6

How to call a class into my extension using autoload. I am using typo3 7.6. From the tutorials I understood that, for typo3 7.6, this can be done in ext_emconf.php file. How to write the same in ext_emconf.php?? My class file is in Classes/class.x.php.

Upvotes: 1

Views: 988

Answers (1)

Dimitri Lavrenük
Dimitri Lavrenük

Reputation: 4879

All classes are automatically registered in an autoload function as long as you follow the code conventions: https://docs.typo3.org/typo3cms/ExtbaseFluidBook/a-CodingGuidelines/Index.html

class.x.php is not a valid filename for a class in Extbase. If you want to create a ClassX then the filename has to be:

/your_extension/Classes/ClassX.php

<?php

namespace YourName\YourExtension;

class ClassX {

}

Note that the extension name also turns in UpperCamelCase. For the vendor part (YourName in the example) you can chose anything that is valid in PHP.

now you can access you class with

$test = new \YourName\YourExtension\ClassX();

Your extension obviously needs to be installed to work.

!!! Keep in mind, that Typo3 only generates the autoload cache when you install / uninstall an extension. If you add new files to an already installed extension you have to delete this cache file manually

/typo3temp/autoload/autoload_classmap.php

To configurate autoloading of classes that do no match the default naming you can create an ext_autoload.php in your extension. Inside the code looks like that:

<?php

return array(
    'Tx_SomeExtension_Someclass' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('some_extension') . 'pi1/someclass.php',
);

If you are developing for Typo3 7.x, keep in mind that pibased is outdated and is only supported with the compatibility extension that brings a lot of drawbacks in performance. I would recommend not to use pibased extension any more.

Upvotes: 4

Related Questions