PHP聚合属性,类中所有对象的

时间:2013-04-16 12:24:59

标签: php class object aggregation

事先承诺成为OO noob ...... 我正在尝试编写一个方法来聚合给定类的所有对象的属性。下面的代码描述了我想要做的事情。

Class TeamMember(){
    function SetScore($value)   {
        $this->score = $value;
    }

    function GetTotalScoreForTeam() {
        //best way to iterate over all the objects to get a sum??????
        return totalScore;
    }
}

$john = new TeamMember();
$john->SetScore('182');

$paul = new TeamMember();
$paul->SetScore('212');

$totalScore = TeamMember->GetTotalScoreForTeam;

谢谢!

2 个答案:

答案 0 :(得分:1)

即使这不是解决此问题的最佳方式,我认为这是您用预先编写的代码解释问题最多的方法:

class TeamMember { // a class is not a function/method; removed the ()
    public static $members = array(); // holds the instances
    public $score; // It is simply good practice to declare your fields (it is not necessary)

    function __construct () {
        self::$members[] = $this; // save the instances accessible for your score-calcuating method
    }

    function setScore ($value) {
        $this->score = $value;
    }

    static function getTotalScoreForTeam () { // a static method is best here
        $totalScore = 0;
        foreach (self::$members as $member) // foreach over the instances holding list
            $totalScore += $member->score;
        return $totalScore;
    }
}

$john = new TeamMember();
$john->setScore('182');

$paul = new TeamMember();
$paul->setScore('212');

$totalScore = TeamMember::getTotalScoreForTeam(); // for static access, use a :: instead of ->

答案 1 :(得分:0)

正如Mike B在评论中所说,您应首先将TeamMember实例分组到Team类的实例中,然后运行一些聚合函数来计算该特定团队的总分,例如

<?php
$team1 = Team::create('Team 1')
    ->add( TeamMember::create('john')->SetScore(182) )
    ->add( TeamMember::create('paul')->SetScore(212) );

$team2 = Team::create('Team 2')
    ->add( TeamMember::create('peter')->SetScore(200) )
    ->add( TeamMember::create('marry')->SetScore(300) );


foo($team1);
foo($team2);

function foo(Team $team) {
    $score = $team->membersReduce(
        function($v, $e) {
            return $v+$e->getScore();
        }
    );

    $members = $team->membersMap(
        function($e) {
            return $e->getName();
        }
    );

    echo 'team : ', $team->getName(), "\r\n";
    echo 'score: ', $score, "\r\n";
    echo 'members: ', join(' | ', $members), "\r\n";
}

class TeamMember {
    protected $score = 0;
    protected $name;

    public static function create($name) {
        return new TeamMember($name);
    }

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

    public function getName() {
        return $this->name;
    }

    public function SetScore($value) {
        $this->score = $value;
        return $this;
    }

    public function GetScore() {
        return $this->score;
    }
}

class Team {
    protected $members = array();
    protected $name;

    public static function create($name) {
        return new Team($name);
    }

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

    public function getName() {
        return $this->name;
    }

    public function add(TeamMember $t) {
        // <-- check if $t already member of team -->
        $this->members[] = $t;
        return $this;
    }

    public function membersReduce($fn) {
        return array_reduce($this->members, $fn);
    }

    public function membersMap($fn) {
        return array_map($fn, $this->members);
    }
}

打印

team : Team 1
score: 394
members: john | paul
team : Team 2
score: 500
members: peter | marry