Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:52:42+00:00 2026-06-17T16:52:42+00:00

Okay, I know. I know… this one is probably an easy one. But, I’m

  • 0

Okay, I know. I know… this one is probably an easy one. But, I’m very new to OOP and want to learn how to recycle my code more efficiently.

I have a class in php, and then another class that extends a previous class like so

class team {
   private $league;
   private $team;
   public $year = 2013;

   function getTeamSeasonRecord($league, $team) {
   // Get given's team record thus far

      $this->record = $record;
     return $record;
}

class game extends team{

  public function __construct()
  {
        global $db;

        if($game_league == "mlb")
        {
            $table = "current_season_games";
        } else
        {
            $table = "".$game_league."_current_season_games";
        }

        $query = "SELECT * FROM ".$table." WHERE game_id = :game_id";
        $stmt = $db->prepare($query);
        $stmt->execute(array(':game_id' => $game_num));
        $count = $stmt->rowCount();

        $this->games_count = $count;

        if($count == 1)
        {
            $this->game_league = $game_league;
            $this->game_num = $game_num;

            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
            {
                $home_team  = $row['home_team'];
                $away_team  = $row['away_team'];
                $game_int   = $row['game_date_int'];
                $game_date  = $row['game_date'];
                $game_time  = $row['game_time'];
            }

            $this->home_team = $home_team;
            $this->away_team = $away_team;
            $this->game_int = $game_int;
            $this->game_date = $game_date;
            $this->game_time = $game_time;
        }
    }

  $team_class = new team($this->game_league, $this->home_team, 2013);
  $record = $team_class->getTeamSeasonRecord($this->game_league, $this->home_team);
  $this->team_record -> $record;
}

Since the class titled “game” is an extension of the class titled “team,” can’t the game class access all of the functions within the scope of the team class? The getTeamSeasonRecord() function that is written in the team class will find the record of any given team. But, for the game class, there are two teams. 1.) the home team and 2.) the away team. I need to find the records of both the home team and the away team. How can I recycle the code so that I don’t have to have the same function in both classes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:52:43+00:00Added an answer on June 17, 2026 at 4:52 pm

    Game should not extend team because a game is not a team. A game is something played by teams, its a different object entirely and inheritance in the way you have modelled should not apply.

    class game{
    
      private $homeTeam;
      private $awayTeam;
    
      function GetHomeTeam()
      {
        return $this->homeTeam;
      }
    
      function GetAwayTeam()
      {
        return $this->awayTeam;
      }
    }
    

    An example of where inheritance could possibly be used (and continuing with the sporting domain)

    class Team
    {
      function GetSeasonRanking(){...}
    }
    
    class SoccerTeam extends Team
    {
      function GetGoalKeeper(){...}
    }
    
    class BaseballTeam extends Team
    {
      function GetPitchers{...}
    }
    

    SoccerTeam and BaseballTeam (subtypes) are both teams (supertype) and have a season ranking however they must extend team to implement the intricacies of the game the team plays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I know this is more text than you want but I literally cannot
Okay, I know this is probably dead simple, but I can't seem to find
Okay I know this is a ridiculously easy question, but for some reason I
Okay, I know this is a Noob question but I cant see to get
Okay, I know that 1) this is probably not possible with CSS alone and
Okay, I know it is weird but when I put this code between <head
Okay so I know I asked a similar question a while ago, but this
Okay, I know this i kinda a vague question. But I have a requirement
Okay, I know this question have been asked plenty of times already, but I
Okay, I know this is a common enough question, but all the solutions I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.