Frage

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;        
        }        
 ?>
War es hilfreich?

Lösung

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;        
    } 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top