John Davidson

php - TYPO3 how use 'use'

0 comments
Message:


I know, that are some stupid questions, but I am a little bit confused.


Above, it is a screenshoot from https://docs.typo3.org/m/typo3/book-extbasefluid/main/en-us/7-Controllers/1-Creating-Controllers-and-Actions.html.


As you can see, in one case there is no '\' before MyVendor, in another one it is. Are both way possible?


In my extensions I have under Classes the following structure.



If I use the use function, It is not necessary to `Classes in the statement.


use MyVendor\ExtName\Controller\MainController is enough?



My Answer:
Yes, both ways are possible in TYPO3. You can use the fully qualified namespace with the backslash before MyVendor, or you can use the use statement at the beginning of your file to import the namespace and then just use the class name without the full namespace in your code.

So in your case, if you have a MainController class in the MyVendor\ExtName\Controller namespace, you can either use:

php
use MyVendor\ExtName\Controller\MainController;

// Then use the class like this
$controller = new MainController();


Or you can directly use the fully qualified namespace in your code like this:

php
$controller = new \MyVendor\ExtName\Controller\MainController();


Both ways will work in TYPO3, so you can choose the one that you prefer or that fits better in your code structure.

Rate this post

4 of 5 based on 2957 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us