斐波那契数列PHP非递归数组实现

概念: 斐波那契数列即表达式为 a(n) = a(n-1)+a(n-2) 其中 a1 =0 a2 = 1 的数列

代码实现功能: 该类实现初始化给出n,通过调用getValue函数得出a(n)的值

<?php                                                                                                                                        
class Fbnq
{
    private $num_count = 0;
    private $Fbnq_arr = array(0, 1);  // 0,1是初始也是默认的值  注意数组下标比数列下标多一
    
    public function __construct($num_count)
    {   
        if (is_numeric($num_count) && $num_count>=0)
        {   
            $this->num_count = $num_count;
        }   
    }   
 
    public function getValue()
    {   
        for($i=2; $i<$this->num_count; $i++)
        {   
            $this->Fbnq_arr[$i] = $this->Fbnq_arr[$i-1] + $this->Fbnq_arr[$i-2];
        }   
        return $this->Fbnq_arr[$this->num_count-1];
    }
}
 
$f = new Fbnq(9);
echo $f->getValue();