mardi 23 août 2016

PHP constructor generates infinite loop when call method inside method

I have a constructor in Laravel Eloquent Model. When I instantiate the Model I've got an infinite loop when the method that constructor calls call another method.

class ChessboardCell extends Model
{
    //...

    public function __construct()
    {
        parent::__construct();

        //...

        $this->putPieces('pawn', 'black', $blackPawns);

        //...
    }

    private function putPieces($piece, $player, $piecesArray)
    {
        //...

        if ($piece == 'pawn') {
            for ($file = 'a'; $file <= 'h'; $file++) {
                $pawnPiece = array_shift($piecesArray);
                $this->putPieceOnBoard($file, $rank, $pawnPiece);
            }
        }

        //...
    }

    public function putPieceOnBoard($file, $rank, $piece)
    {
        $chessboardCell = ChessboardCell::where('file', $file)
            ->where('rank', $rank)
            ->first();

        if (is_null($chessboardCell)) {
            // Gera excessão: não pegou a célula
        }
        $chessboardCell->current_piece = $piece->id;

        $chessboardCell->save();
    }

}

After calling $this->putPieceOnBoard inside putPieces function gets infinite loop.

What happens here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire