Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

Parola chiave insteadof

Risolve un conflitto tra metodi dei trait scegliendo un’implementazione al posto delle concorrenti specificate.

Versione: PHP 8.4; repository runtime, production runtime not attested

Sintassi

use Left, Right { Left::label insteadof Right; }

Risolve un conflitto tra metodi dei trait scegliendo un’implementazione al posto delle concorrenti specificate. Il metodo escluso può essere importato con un alias tramite as. Un conflitto irrisolto causa un errore fatale.

trait Left {public function label(){return "L";}} trait Right {public function label(){return "R";}}
class Sign {use Left,Right {Left::label insteadof Right; Right::label as other;}}
$s=new Sign(); echo $s->label(),$s->other();
LR

Esempi

<?php
trait Left {public function label(){return "L";}} trait Right {public function label(){return "R";}}
class Sign {use Left,Right {Left::label insteadof Right; Right::label as other;}}
$s=new Sign(); echo $s->label(),$s->other();

Risultato

LR

Fonti