2
\$\begingroup\$

I have a class which creates a new match. My problem is that I am sure the code is unnecessary long and can get much improved (just keep in mind it is beginner level).

Specifically, I would like to know:

  • Can I change the below into 1 setter and 1 getter method?
  • I would like to use the rand() function for match ID can I do this inside the setter function of setMatchId or should it be done outside of the class?
  • When I upload the class to a db I assume I would need to create a new method and code the DB upload inside the method or should this be done outside of the class.

<?php
class match{
private $matchId;
private $team1;
private $team2;
private $venue; 

function __construct($pMatchId, $pTeam1, $pTeam2, $pVenue){
    $this->matchId = $pMatchId;
    $this->team1 = $pTeam1;
    $this->team2 = $pTeam2;
    $this->venue = $pVenue;
}
    function setMatchId($pMatchId){
        $this->matchId = $pMatchId;

    }

    function getMatchId(){
        return $this->matchId;  

    }
    function setTeam1($pTeam1){
        $this->team1 = $pTeam1; 
    }
    function getTeam1(){
        return $this->team1;    

    }

    function setTeam2($pTeam2){
        $this->team2 = $pTeam2; 
    }
    function getTeam2(){
        return $this->team2;    

    }
    function setVenue($pVenue){
        $this->venue = $pVenue; 

    }
    function getVenue(){
        return $this->venue;    

    }

} // c;lass match

$x = new match("1", "Patriots", "Chargers", "Newlands");
echo $x->getMatchId();
echo'<br />';
echo $x->getTeam1();
echo'<br />';
echo $x->getTeam2();
echo'<br />';
echo $x->getVenue();
?>
\$\endgroup\$
1
2
\$\begingroup\$

Can I change the below into 1 setter and 1 getter method?

You could use magic getters and setters, but you probably shouldn't (see the top-rated answer in that question; additionally, with the primitive approach, you would make all your private fields accessible from the outside).

You should also ask yourself if you even need setters. If the properties of the object never change after construction, you should make it immutable, as immutable objects are easier to handle (you know that they never change).

I would like to use the rand() function for match ID can I do this inside the setter function of setMatchId or should it be done outside of the class?

You should not add a method setMatchId() that doesn't accept an argument, but just generates the id. Setters should set something, not generate something.

On the other hand, a method such as regenerateMatchId would be perfectly acceptable (although I'm not sure why you would want the id to be random).

When I upload the class to a db I assume I would need to create a new method and code the DB upload inside the method or should this be done outside of the Class

By "upload to the db", you mean save in the database? Basically, you would have three options:

  • add the "save to db" method to this class
  • create a MatchDAO which saves matches to the db
  • use an ORM such as doctrine

Misc

  • class names should ideally start with an upper-case letter (see for example PSR-1)
  • your indentation is off, as is your vertical whitespace
  • what does the p in your argument names stand for? It's unclear, I would just remove it.
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.