magento 2 how to call my custom method in test.php at root folder?
题
I would like to know, I have a custom method in test.php at magento2 root folder i would like to call my custom method how to do that? i have written a sample test script below please suggest me here thanks.
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
ini_set('display_errors', 1);
//Here how to call my custom method ????
public function getCustomMethod($i)
{
echo $i;
(or)
return $i;
}
?>
解决方案
Since you are not going to create any class in this php file, you can directly define and declare the method as like below.
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
ini_set('display_errors', 1);
$i = "Hello";
getCustomMethod($i);
function getCustomMethod($i)
{
echo $i;
(or)
return $i;
}