Foros del Web » Programando para Internet » PHP »

Asignar objeto a otro objeto en php

Estas en el tema de Asignar objeto a otro objeto en php en el foro de PHP en Foros del Web. Tengo el siguiente código donde hay dos clases 'jugador' y 'equipo', las dos clases ya tienen 3 objetos creados por defecto cada uno. Lo que ...
  #1 (permalink)  
Antiguo 25/01/2019, 14:01
 
Fecha de Ingreso: mayo-2015
Mensajes: 30
Antigüedad: 8 años, 11 meses
Puntos: 0
Asignar objeto a otro objeto en php

Tengo el siguiente código donde hay dos clases 'jugador' y 'equipo', las dos clases ya tienen 3 objetos creados por defecto cada uno.

Lo que estoy tratando de hacer es asignar un jugador a un equipo, pero no estoy seguro de cómo hacerlo.

Estas son las clases :

team.php
Código PHP:
Ver original
  1. <?php session_start();
  2.  
  3. class Team {
  4.  
  5.     private $teams;
  6.     private $name;
  7.  
  8.     public function __construct($name){
  9.  
  10.         $this->name = $name;
  11.         $this->teams = array();
  12.     }
  13.  
  14.     public function getName(){
  15.         return $this->name;
  16.     }
  17.  
  18.     public function addTeam($oneTeam){
  19.         $this->teams[] = $oneTeam;
  20.  
  21.         return $this;
  22.     }
  23.  
  24.     public function printTeams(){
  25.  
  26.         echo "<br>"."<br>";
  27.         echo "<b>Teams available</b>";
  28.         echo "<br>"."<br>";
  29.  
  30.         foreach($this->$teams as $team){
  31.  
  32.             echo $team->getName() . " ";
  33.             echo "<br>";
  34.         }
  35.     }
  36.  
  37. }
  38.  
  39.  
  40. // Load the team data of the session and if it does not exist create a new team.
  41. function loadDataTeam(){
  42.     return isset($_SESSION['team']) ? $_SESSION['team'] : new Team();
  43. }
  44.  
  45. // Save the team data in the session.
  46. function saveDataTeam($team){
  47.     $_SESSION['team'] = $team;
  48. }
  49.  
  50. $team = new Materia();
  51.  
  52. //teams by default.
  53. $team->addTeam(new Team("Team 1"));
  54. $team->addTeam(new Team("Team 2"));
  55. $team->addTeam(new Team("Team 3"));
  56.  
  57. saveDataTeam($team);
  58.  
  59. ?>

player.php

Código PHP:
Ver original
  1. <?php session_start();
  2.     include_once("team.php");
  3.     class Player {
  4.         private $players;
  5.         private $name;
  6.         public function __construct($name){
  7.             $this->name = $name;
  8.             $this->players = array();
  9.         }
  10.         public function getName(){
  11.             return $this->name;
  12.         }
  13.         public function addPlayer($onePlayer){
  14.             $this->players[] = $onePlayer;
  15.             return $this;
  16.         }
  17.         public function printPlayers(){
  18.             foreach($this->players as $player){
  19.                 echo $player->getName() . " ";
  20.                 echo "<br>";
  21.             }
  22.         }
  23.     }
  24.     // Load the player data of the session and if it does not exist create a new player.
  25.     function loadDataPlayer(){
  26.         return isset($_SESSION['player']) ? $_SESSION['player'] : new Player();
  27.     }
  28.     // Save the player data in the session.
  29.     function saveDataPlayer($player){
  30.         $_SESSION['player'] = $player;
  31.     }
  32.     $player = new Player();
  33.     //Players by default.
  34.     $player->addPlayer(new Player("Player 1"));
  35.     $player->addPlayer(new Player("Player 2"));
  36.     $player->addPlayer(new Player("Player 3"));
  37.     saveDataPlayer($player);
  38.     ?>

index.php

Código PHP:
Ver original
  1. <html>
  2. <head>
  3.     <title>Asignar jugador a equipo</title>
  4.     <meta charset="UTF-8"/>
  5. </head>
  6. <body>
  7.  
  8.     <?php
  9.  
  10.     form();
  11.  
  12.     include_once("team.php");
  13.     include_once("player.php");
  14.  
  15.     $player = new Player();
  16.     $team = new Team();
  17.  
  18.     $player = loadDataPlayer();
  19.     $team = loadDataTeam();
  20.  
  21.     function form(){
  22.  
  23.         echo '<FORM METHOD="POST" style="text-align: center; margin-top: 73px;">
  24.        <h2>Asignar jugador a equipo</h2>
  25.        <label>Introduce el nombre del jugador : </label><INPUT class="form" TYPE = "text" NAME = "name"> <br>
  26.        <label>Introduce el nombre del equipo : </label><INPUT class="form" TYPE = "text" NAME = "team"> <br><br>
  27.        <INPUT class="form" TYPE = "submit" VALUE = "Send" name="action">
  28.        </ FORM>';
  29.     }
  30.  
  31.     ?>
  32.  
  33. </body>
  34. </html>

Etiquetas: html, objeto
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:48.