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 7918529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:32:35+00:00 2026-06-03T15:32:35+00:00

Im writing a football manager simulator with php, [ HARD AlGORITHMS !] i have

  • 0

Im writing a football manager simulator with php, [ HARD AlGORITHMS !]

i have 3 class :

Player

class Player {

protected $name;

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

Team

class Team {

protected $name;

protected $players = array();


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

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

public function addPlayer($player) {
    $this->players[] = $player;
}

public function getPlayers() {
    print_r($this->players);
}

public function getOpponetsPosition() {
    GAME::getOpponetPlayersPosition();
}

and Game

class Game {

protected $t1;
protected $t2;

function setTeams($team1,$team2) {
    $this->t1 = $team1;
    $this->t2 = $team2;
}

function getOpponetPlayersPosition() {
        $this->t1->getPlayers();
}

}

and main script

require_once 'classes/CPlayer.php';
require_once 'classes/CTeam.php';
require_once 'classes/CGame.php';

$game = new Game;

$team1 = new Team;
$team1->setName("PO-1");
$team2 = new Team;
$team2->setName("PO-2");

$p1 = new Player;
$p2 = new Player;


$p1->addAttr("payam babaiy");
$p2->addAttr("parsa babaiy");

$team1->addPlayer($p1);
$team2->addplayer($p2);

$game->setTeams($team1,$team2);

$team1->getOpponetsPosition();

I Need to get all player positions in game with getOpponetsPosition() function in Team class

but it doesnt return values which i inputed in my main script.
am i doing this right ? is this a good approach for app im writing ?

  • 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-03T15:32:36+00:00Added an answer on June 3, 2026 at 3:32 pm

    Your approach is good, a few points:

    Use constructors, they can make your life easier:

    class Player {
    
        protected $name;
        
        public function __construct($name) {
            $this->name = $name;
        }
    
        public function addAttr($name) {
            $this->name = $name;
        }
    }

    And then

    new Player("Lionel Messi");
    

    Constructors also ensure you don’t get players/teams with empty names! You get better control over what gets in your classes!

    Don’t mix static and normal code

    Your getOpponentsPosition function is not correct

    public function getOpponetsPosition() {
        GAME::getOpponetPlayersPosition();
    }
    

    In fact, it shouldn’t even be here, it’s not the Team‘s job to fetch the other team’s positions, that would be the game’s, since it contains both.

    See this gist for how I would have accomplished your goal.

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

Sidebar

Related Questions

Writing some classes for a Framework extension, and I have the following code: public
Writing my first PHP Class and came across an issue, the following is my
Writing a PHP app and have several classes that only have static methods (no
I have this PHP: <?php $data = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml'); $xml = simplexml_load_string($data); $data1 = file_get_contents('http://www.skysports.com/rss/0,20514,11661,00.xml');
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
Writing a lexer of .java source files in Java. I have a stream of
Writing code in PHP, how can I ensure the require_once path of a script
I'm writing a simple football database, and in that database, I want teams are
//Writing a letter #include <iostream> using namespace std; int main() { string first_name; //Name
Writing a function to generate and push some random data inside of the unknown

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.