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 :)

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);
     }
     }
| improve this answer | |
  • I answered you quickly why you didn't accept. – mahendra j Sep 7 '18 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 
     }
 }
| improve this answer | |
  • Say i wanted to add a multiply method in the Calculator class, how would i then call both ? – Greg Sep 7 '18 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) – Ansar Husain Sep 7 '18 at 11:34
  • When i print, echo $this->calculator->divide(10,5); echo $this->calculator->multiply(10,5); i only get one number. Why? – Greg Sep 7 '18 at 11:38
  • sorry use public function multiply($number1, $number2) { return $number1*$number2; } – Ansar Husain Sep 7 '18 at 11:42
  • You need to add echo "<br/>"; after each function in controller – Ansar Husain Sep 7 '18 at 11:59

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.