如何使用数组类属性在不同的自类的方法?

问题描述:

我有一个这样的类:如何使用数组类属性在不同的自类的方法?

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值填补。

下面的代码我有一个方法叫费(),使以及不同的计算和填充值的一类,这是在我的前端outputed过的chargesArray财产。

现在,我必须改进我的功能,所以我必须在我的charges()方法中实现一些东西,以便使我的constantAmortization()方法中的每次迭代中的资本初始/剩余资金的百分比值保持不变。

我该怎么办?在我的想法我必须用我的费用里面result_array属性()方法,迭代这个数组,并根据资本和剩余价值做一些计算,但它给了我的错误,当我尝试使用内/显示result_array charges()方法。我必须做什么?

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

大在这里的解释:在ifarzan http://php.net/manual/en/language.oop5.php通过farzan科网¶

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" 
?> 

重要提示:您必须围绕函数名{和}或PHP会认为你一个重新调用对象“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()}; 
?> 

您可以使用上面访问成员函数,以及注资方式:

<?php 
print $foo->{'aMemberFunc'}(); // Prints "Inside `aMemberFunc()`" 
print $foo->{$foo->aFuncName}(); // Prints "Inside `aMemberFunc()`" 
?>