magento 2 how to call any block function in phtml

Try like this.

For ex your block class is

<?php
namespace Company\Helloworld\Block;
use Magento\Framework\View\Element\Template;

class Main extends Template
{
    public function getMyCustomMethod()
    {
        return '<b>I Am From MyCustomMethod</b>';
    }
}

then in any phtml file you can use following code to get method of this block.

<?php
$blockObj= $block->getLayout()->createBlock('Company\Helloworld\Block\Main');
echo $blockObj->getMyCustomMethod();
?>

Hope this helps you.


If the template is linked to the block, for example:

<block class="Vendor\Module\Block\Name" name="name" template="Vendor_Module::name.phtml"/>

And you have a public method myMethod() defined in Vendor\Module\Block\Name you can call the following in name.phtml :

$block->myMethod();