如何在我的函数sorfOfPaidServices中访问属性?

时间:2017-04-17 13:04:00

标签: php arrays class

我无法在func sortOfPaidServices()中对数组进行排序。属性$costs在类PaidService中声明,但我需要在另一个类PaidServicesHandler中对数组进行排序,并且我不知道如何在函数{{1}中转向我的属性}。我需要通过属性$ costs的冒泡排序数组。现在我有2个相同的输出(来自func sortOfPaidServicesoutput())。

PHP:

sortOfPaidServices()

HTML / PHP:

abstract class PaidService{
    public $id;
    public $name;
    public $costs;

    public function __construct($id, $name, $costs){
        $this->id = $id;
        $this->name = $name;
        $this->costs = $costs;
    }

    abstract public function calculateAverageMonthlyCosts();

    public function printAtribute(){
        echo $this->id."/".$this->name."/".$this->costs;
        echo "<br/>";
    }
}

class FixedMonthlyCostPaidService extends PaidService{
    public function calculateAverageMonthlyCosts(){
        return $avMonCosts = 30 * 24 * $this->costs;
    }
}

class FixedHourlyCostPaidService extends PaidService{
    public function calculateAverageMonthlyCosts(){
        return $this->costs;
    }
}

$test = new FixedMonthlyCostPaidService (2, 'xyi', 2);
echo $test -> calculateAverageMonthlyCosts();

$test1 = new FixedHourlyCostPaidService (2, 'xyi', 2);
echo " ".$test1 -> calculateAverageMonthlyCosts();

class PaidServicesHandler{
    public  $array = null;

    function output(){
        foreach ($this->array as $value) {
            $value->printAtribute();
        }
    }

    function sortOfPaidServices(){
        for ($i=0; $i < 7; $i++) { 
            for ($j=$i+1; $j < 7; $j++) { 
                if($this->array[i]->costs>$this->array[i]->costs){
                    $temp = $this->array[j];
                    $this->array[j] = $this->array[i];
                    $this->array[i] = $temp;
                    echo $this->array[i];
                }
            }
        }

        echo "</br>";
        foreach ($this->array as $value) {
            $value->printAtribute();
        }
    }
}

$paidServisec = new PaidServicesHandler();  

$paidServisec->array = array(
    new FixedHourlyCostPaidService('service1', 'Google Orkut', 11),
    new FixedHourlyCostPaidService('service2', 'Google Voice', 9.4),
    new FixedMonthlyCostPaidService('service5', 'YouTube', 8064),   
    new FixedHourlyCostPaidService('service3', 'Mandrill', 11.2), 
    new FixedHourlyCostPaidService('service4', 'Google Finance', 7.8),
    new FixedMonthlyCostPaidService('service7', 'Google Building Maker', 5347),
    new FixedMonthlyCostPaidService('service6', 'LinkedIn', 6863)
);

$paidServisec->output();
$paidServisec->sortOfPaidServices();

0 个答案:

没有答案