Asignar objeto a otro objeto en php
Publicado por YZW (2 intervenciones) el 25/01/2019 20:52:18
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
player.php
index.php
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php session_start();
class Team {
private $teams;
private $name;
public function __construct($name){
$this->name = $name;
$this->teams = array();
}
public function getName(){
return $this->name;
}
public function addTeam($oneTeam){
$this->teams[] = $oneTeam;
return $this;
}
public function printTeams(){
echo "<br>"."<br>";
echo "<b>Teams available</b>";
echo "<br>"."<br>";
foreach($this->$teams as $team){
echo $team->getName() . " ";
echo "<br>";
}
}
}
// Load the team data of the session and if it does not exist create a new team.
function loadDataTeam(){
return isset($_SESSION['team']) ? $_SESSION['team'] : new Team();
}
// Save the team data in the session.
function saveDataTeam($team){
$_SESSION['team'] = $team;
}
$team = new Materia();
//teams by default.
$team->addTeam(new Team("Team 1"));
$team->addTeam(new Team("Team 2"));
$team->addTeam(new Team("Team 3"));
saveDataTeam($team);
?>
player.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php session_start();
include_once("team.php");
class Player {
private $players;
private $name;
public function __construct($name){
$this->name = $name;
$this->players = array();
}
public function getName(){
return $this->name;
}
public function addPlayer($onePlayer){
$this->players[] = $onePlayer;
return $this;
}
public function printPlayers(){
foreach($this->players as $player){
echo $player->getName() . " ";
echo "<br>";
}
}
}
// Load the player data of the session and if it does not exist create a new player.
function loadDataPlayer(){
return isset($_SESSION['player']) ? $_SESSION['player'] : new Player();
}
// Save the player data in the session.
function saveDataPlayer($player){
$_SESSION['player'] = $player;
}
$player = new Player();
//Players by default.
$player->addPlayer(new Player("Player 1"));
$player->addPlayer(new Player("Player 2"));
$player->addPlayer(new Player("Player 3"));
saveDataPlayer($player);
?>
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<html>
<head>
<title>Asignar jugador a equipo</title>
<meta charset="UTF-8"/>
</head>
<body>
<?php
form();
include_once("team.php");
include_once("player.php");
$player = new Player();
$team = new Team();
$player = loadDataPlayer();
$team = loadDataTeam();
function form(){
echo '<FORM METHOD="POST" style="text-align: center; margin-top: 73px;">
<h2>Asignar jugador a equipo</h2>
<label>Introduce el nombre del jugador : </label><INPUT class="form" TYPE = "text" NAME = "name"> <br>
<label>Introduce el nombre del equipo : </label><INPUT class="form" TYPE = "text" NAME = "team"> <br><br>
<INPUT class="form" TYPE = "submit" VALUE = "Send" name="action">
</ FORM>';
}
?>
</body>
</html>
Valora esta pregunta
0