2017-07-14 1 views
1

私はそのようなクラスがあります。だから、異なる自己クラスメソッドで配列クラスプロパティを使用するには?

class Calculator 
{ 
    protected $date; 
    protected $capital; 
    protected $rate; 
    protected $frequency; 
    protected $duration; 
    protected $charges; 
    protected $forcedPayments; 
    protected $result_array; 
    protected $chargesArray; 


public function constantAmortization($date, $capital, $rate, $duration, $frequency, $charges) 
{ 
     $allPayments = $allInterests = array(); 
     $amortization = $capital/$duration; 
     $interestTotal = 0; $amortizationTotal = 0; 
      for($i = 0; $i < $duration; $i++){ 
       $interest = $capital * ($rate/100)/$frequency; 
       array_push($allInterests,$interest); 
       $payment = $amortization + $interest; 
       array_push($allPayments,$payment); 
       $remaining = $capital - $amortization; 
       //calculate totals for table headers output in twig : 
       $interestTotal += $interest; $amortizationTotal += $amortization; 
       $inverseCapital = $capital * -1; 
       $paymentTotal = $amortizationTotal + $interestTotal;  

       $this->result_array[$i] = result_array(
          'Date' => $date->format('d/m/y'), 
          'Capital' => $capital, 
          'Rate' => $rate, 
          'Interest' => $interest, 
          'Payment' => $payment, 
          'Amortization' => $amortization, 
          'Remaining' => $remaining, 
          'InterestTotal' => $interestTotal, 
          'AmortizationTotal' => $amortizationTotal, 
          'PaymentTotal' => $paymentTotal, 
          'InverseCapital' => $inverseCapital, 
        ); 

       $capital = $remaining; 
       $months = (12/$frequency). ' months'; 
       $date->modify($months); 
     } 
    } 

は、この方法では、クラスのプロパティに基づいて、クラスの「array_result」プロパティは私のフロントエンドには、後にoutputedされている値で満たされています。

私はcharge()というメソッドを用意しています。このメソッドは異なる計算を行い、クラスのchargesArrayプロパティに値を入力します。これもフロントエンドで出力されます。

私は関数を改良しなければならないので、私はconstantAmortization()メソッドの各繰り返しで、初期値のパーセント/残量として課金の値を作るために、charges()メソッドで何かを実装する必要があります。

どうすればいいですか?私の考えでは、私のcharges()メソッド内でresult_arrayプロパティを使用して、この配列を反復し、いくつかの計算を行うために大文字と残りの値に基づいている必要がありますが、 charges()メソッド私がしなければならないこと?ここ

答えて

0
$Calc = new Calculator; 
$output = Calc::constantAmortization($date, $capital, $rate, $duration, $frequency, $charges); 
/* or */ 
$output = Calc->constantAmortization($date, $capital, $rate, $duration, $frequency, $charges); 

グレート説明:ifarzanドットでfarzanによってhttp://php.net/manual/en/language.oop5.phpコム¶

PHP 5メンバ変数とメンバ関数へのアクセスに非常に非常に柔軟です。これらのアクセス方法は一目では珍しく不必要に見えるかもしれません。しかし、時にはとても役に立ちます。特にSimpleXMLのクラスとオブジェクトを扱うときに便利です。私はSimpleXML関数リファレンスセクションに同様のコメントを投稿しましたが、これはより包括的です。

は、私はすべての例のための基準として、以下のクラスを使用します。

<?php 
$element = 'aMemberVar'; 
print $foo->$element; // prints "aMemberVar Member Variable" 
?> 

または使用機能:

<?php 
class Foo { 
    public $aMemberVar = 'aMemberVar Member Variable'; 
    public $aFuncName = 'aMemberFunc'; 


    function aMemberFunc() { 
     print 'Inside `aMemberFunc()`'; 
    } 
} 

$foo = new Foo; 
?> 

あなたが名前として別の変数を使用して、オブジェクトのメンバ変数にアクセスすることができます

<?php 
function getVarName() 
{ return 'aMemberVar'; } 

print $foo->{getVarName()}; // prints "aMemberVar Member Variable" 
?> 

重要な注意:関数名を{と}で囲む必要があります。オブジェクト "foo"のメンバ関数を呼び出しています。

あなたが同様に定数やリテラルを使用することができます。

<?php 
define(MY_CONSTANT, 'aMemberVar'); 
print $foo->{MY_CONSTANT}; // Prints "aMemberVar Member Variable" 
print $foo->{'aMemberVar'}; // Prints "aMemberVar Member Variable" 
?> 

をあなたは同様に他のオブジェクトのメンバを使用することができます。

<?php 
print $foo->{$otherObj->var}; 
print $foo->{$otherObj->func()}; 
?> 

あなたにもメンバ関数にアクセスするために、上記のmathodsを使用することができます。

<?php 
print $foo->{'aMemberFunc'}(); // Prints "Inside `aMemberFunc()`" 
print $foo->{$foo->aFuncName}(); // Prints "Inside `aMemberFunc()`" 
?> 
関連する問題