2

Say I want to call a function from a Block while inside my Controller class which outputs on screen, how would i go about this?

Code for New/Module/Controller/Hello/World.php :

  <?php
  namespace New/Module/Controller/Hello/;
  class World extends \Magento\Framework\App\Action\Action 
  {

 public function execute()
 {
    echo '<p>"Hello World"</p>';
    var_dump(__METHOD__);

    divide(10,1);  <-- trying to do this 
 }
 }

Code for New/Module/Block/Calculator.php

<?php 
namespace New\Module\Block; 
class Calculator{ 
public function divide($number1, $number2) 
{ 
return $number1/$number2; 
}
public function multiply($number1,$number2){
return $number1*$number2; 
} 
public function add($number1,$number2){ 
return $number1+$number2;
}  
public function sub($number1,$number2){
return $number1-$number2;  
} 
} 

I'm attempting to create a calculator and I'm not even sure this is the best way to go about calling functions to output on screen.

Any help would be much appreciated :)

2 Answers 2

3

You can call block methods by creating instance of that class.

<?php
      namespace New/Module/Controller/Hello/;
      class World extends \Magento\Framework\App\Action\Action 
      {


        /**
         * Calculator
         *
         * @var \New\Module\Block\Calculator
         */
        public $calculator;

        public function __construct(
        \Magento\Framework\App\Action\Context $context, 
        \New\Module\Block\Calculator $calculator
        ) {
            $this->calculator = $calculator;
            return parent::__construct($context);
        }

     public function execute()
     {
        echo '<p>"Hello World"</p>';
        var_dump(__METHOD__);

        //divides(10,1);  <-- trying to do this 

        $this->calculator->divide(10,1);
     }
     }
1
  • I answered you quickly why you didn't accept. Commented Sep 7, 2018 at 12:23
3

Use Below code for it

<?php
  namespace New/Module/Controller/Hello/;
  class World extends \Magento\Framework\App\Action\Action 
  {
    protected $_calculator;

    public function __construct(
         \Magento\Framework\App\Action\Context $context,
         \New\Module\Block\Calculator $calculator
         ){
        $this->_calculator=$calculator;
        parent::__construct($context);
    }
     public function execute()
     {
        echo '<p>"Hello World"</p>';
        var_dump(__METHOD__);

        $this->_calculator->divide(10,1);  <-- trying to do this 
     }
 }
5
  • Say i wanted to add a multiply method in the Calculator class, how would i then call both ? Commented Sep 7, 2018 at 11:31
  • <?php namespace New\Module\Block; class Calculator{ public function divide($number1, $number2) { return $number1/$number2; } public function multiply($number1, $number2) { return $number1/$number2; } } $this->_calculator->multiply(10,1) Commented Sep 7, 2018 at 11:34
  • When i print, echo $this->calculator->divide(10,5); echo $this->calculator->multiply(10,5); i only get one number. Why? Commented Sep 7, 2018 at 11:38
  • sorry use public function multiply($number1, $number2) { return $number1*$number2; } Commented Sep 7, 2018 at 11:42
  • You need to add echo "<br/>"; after each function in controller Commented Sep 7, 2018 at 11:59

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.