0

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?.

<?php 

namespace Mage\Calculator\Model; 

class Calculator{ 

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

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

}

Cheers

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

3
  • So like this, <h2><?php $this->divide($x,$y);?></h2> ?
    – Greg
    Sep 6 '18 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
    – Aamir
    Sep 6 '18 at 14:25
  • ok, try first to display some string then check if you get it well public function divide() { return "hellllo"; }
    – Aamir
    Sep 6 '18 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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