1 <? php 2 3 / * * 4 5 生成 : <form id = "form"> <label> <b> 用户 名 : </b> <input name = "usrname"> </label> </form> 6 7 $ form = Html :: tag ('form') -> attr (['id' => 'form']); 8 $ label = Html :: tag ('etiqueta'); 9 $ b = Html :: etiqueta ('b'); 10 $ input = Html :: tag ('input') -> attr ('name', 'usrname'); 11 12 echo $ form-> add ($ label-> add ($ b-> add ('用户 名 :')) -> add ($ input)) -> make (); ; 19 protegidos $ attr = []; 20 protegidos $ tag = '' ; 21 elementos $ protegidos = []; 22 public $ closings = ['br', 'hr', 'input', 'source', 'area', 'base', 'link', 'img', 'meta', 'basefont', 'param', 'col', 'frame', 'embed' ]; 23 24 función protegida __construct ( $ etiqueta ) { 25 26 $ this -> etiqueta = $ etiqueta ; $ valor ) { 30 31 devuelve nueva estática ( $ valor ); 32 } 33 34 función pública attr ( $ name , $ value = '' ) { 35 36 if ( is_array ( $ name )) { 37 $ this -> attr = $ name + $ this -> attr; 38 } else { 39 $ this -> attr [ $ name ] = $ value ; 40 } 41 42 devuelve $ this ; 43 } 44 45 función pública add ( $ element ) { 46 47 $ this -> elements [] = $ element ; 48 devuelven $ esto ; 49 } 50 51 función pública make () { 52 53 $ attr = array_reduce ( array_keys ( $ this -> attr), function ( $ carry , $ item ) { 54 $ carry . = ''. $ Item . '='. '"'. $ This- > attr [ $ item ]. '"' ; 55 return $ carry ; 56 }); 57 58 $ html = '<' . $ this- > tag. $ attr . '>' ; 59 // Iterar a través de los elementos secundarios y llamar al método make para formar un foreach 60 recursivo ( $ this- >elementos como $ element ) { 61 62 if ( is_object ( $ element )) { 63 $ element = $ elemento -> make (); 64 } 65 $ html . = $ Elemento ; 66 } 67 68 if (! In_array ( $ this -> etiqueta, $ this -> cierres)) $ html . = '</'. $ this -> tag. '>' ; 69 70 devuelve $ html ; 71 72 } 73 74 75 }