I've created a new module in magento 2 and I want to add some additional files so that I can create a calculator. I'm unsure where to create a file that calls the functions of my calculator class and where I can pass data into the functions.

Say I have a php file as follows, what would be my code and where would I be creating the file so that I can all these function?.


namespace Mage\Calculator\Model; 

class Calculator{ 

public function divide($x, $y) 
    return $x/$y; 

public function multiplication($x, $y) 
    return $x*$y;



1 Answer 1


Put that functions in a block instead model, then you can call them in your phtml like this : echo $block->divide($x, $y) or echo $this->divide($x, $y)

Block path app/code/{Vendor}/{Module}/Block/Name.php

  • So like this, <h2><?php $this->divide($x,$y);?></h2> ?
    – Greg
    Sep 6, 2018 at 14:23
  • Yes, why not but with some echo it's better :) , ensures that you create well your block and the phtml should be in the same module
    – PЯINCƎ
    Sep 6, 2018 at 14:25
  • ok, try first to display some string then check if you get it well public function divide() { return "hellllo"; }
    – PЯINCƎ
    Sep 6, 2018 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.