3
I want to assign objects to a array
with array_push
, but it’s making a mistake:
Warning: array_push() expects Parameter 1 to be array, null Given in
My class :
<?php
//PREPARA UMA RODADA
class Rodada
{
$partidas = array();
//PREENCHE O ARRAY
public function preencheRodada($partidas, Partida $partida)
{
array_push($partidas, $partida);
}
//RETORNA O ARRAY DE PARTIDAS
public function getRodada()
{
return $partidas;
}
}
test class
require_once('../logica/models/Time.php');
require_once('../logica/models/Partida.php');
require_once('../logica/models/Rodada.php');
$time1 = new Time("SANTOS FC");
$time2 = new Time("BARCELONA FC");
$partida1 = new Partida($time1, $time2);
$partida1->setGolsTime1(2);
$partida1->setGolsTime2(3);
$rodada1 = new Rodada();
$rodada1->preencheRodada($partida1);
$partidasDaRodada = $rodada1->getRodada();
It’s easier to use
$this->partidas[] = $partida
. It’s faster than using the functionarray_push
and it does the same thing– Wallace Maxters
Some of the answers served you, if yes you can accept it ???
– novic