前提・実現したいこと
前回(http://teratail.com/questions/238438)のものを、
クラスにコンストラクタを設定しないで呼び出したいのですが、Arrayとなってしまいします。
var_dumpでNULLになっているということは、値をうまく変数に送れていないのでしょうか?
それとも、呼び出し方が間違っているのでしょうか?
ソースコード
php
1<?php 2 class Item { 3 private $taxRate = 0.08; 4 public $price; 5 public $included = false; 6 7 public function tax( int $price, $included = false ) { 8 $this->taxRate = $taxRate; 9 $this->price = $price; 10 $this->included = $included; 11 12 if ( $this->included ) { // true = 内税 13 $array = [ 14 'tax' => $this->price * $this->taxRate, 15 'price' => $this->price - ( $this->price * $this->taxRate ) 16 ]; 17 return $array; 18 } else { // false = 外税 19 $array = [ 20 'tax' => $this->price * $this->taxRate, 21 'price' => $this->price 22 ]; 23 return $array; 24 } 25 } 26 } 27 28 $item = new Item(); 29 30 echo $item->tax( 100, true ).PHP_EOL; // true = 内税 31 echo $item->tax( 100, false ).PHP_EOL; // false = 外税 32 var_dump($item->tax); 33?>
エラーメッセージ
Array Array NULL
アドバイスなど、いただけますと幸いです。
よろしくお願いいたします。
回答3件
あなたの回答
tips
プレビュー