V
V
VicTHOR2019-04-07 21:26:45
PHP
VicTHOR, 2019-04-07 21:26:45

How to determine the use of constructor variables in a function?

Yes, a newbie question, show the errors)
Functions, classes are made according to the instructions, i.e. it is said to implement a function with the acceptance of variables, but also a constructor with the reception of variables .. but if I drive it into the constructor, why then pass the functions separately? meaning is lost. How to pass the same variables that are passed to the constructor?
I tried use __construct - it gave a syntax error saying unexpected use, I tried to pass exp1(&$a, &$b, &$c) zero reaction .. I tried to declare public $a, $b, $c both in F1 and in abstract, I thought It may be necessary to declare, but it did not help either.
Thanks in advance)

Abstract class BaseMath
{
    protected function exp1($a, $b, $c)
    {
        return $this->$a*pow($this->$b,$this->$c);
    }

    protected function exp2($a, $b, $c)
    {
        return pow(($this->$a/$this->$b),$this->$c);
    }

    protected abstract static function getValue();
}

Class F1 extends BaseMath
{
    public function __construct($a, $b, $c)
    {
        $this->$a = $a;
        $this->$b = $b;
        $this->$c = $c;
    }
    public static function getValue()
    {
        $f = $a*pow($b, $c)+pow(((pow(($a/$c), $b))%3), min($a,$b,$c));
        echo $f;
        return true;
    }

}
$func = new F1(4,7,12);
$funk->exp1();
$funk->exp2();
$funk::getValue();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Popov, 2019-04-07
@VicTHOR

abstract class BaseMath {
  protected $a;
  protected $b;
  
  public function calculate() {
    return $this->a + $this->b;    
  }
}

class Math extends BaseMath {
  public function __construct($a, $b) {
     $this->a = $a;
      $this->b = $b;
    }
}


$math = new Math(1, 2);
echo $math->calculate();

Can. Everything works successfully.
PS The code case is a test one. I do not recommend working like this in combat.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question