Class Html

  • Creatore Discussione Creatore Discussione Mr. PHP
  • Data di inizio Data di inizio

Mr. PHP

Utente Attivo
31 Gen 2013
141
0
0
Ho questa classe Html il problema e che non funziona come dovrebbe, qualcuno sa come mai ???
PHP:
class Html {
	
	private $tag = '', $open_tag = '', $close_tag = '';
	
	public function tag( $tag, $value, $attr = '' ){

		if( is_object( $value ) ){
			
			$this->open_tag .= '<'.$tag.$this->attr( $attr ).'>';
			
			$this->close_tag .= '</'.$tag.'>';
			
		}else{
			
			if( $this->open_tag != '' ){
				
				$this->tag .= $this->open_tag;
				
				$this->open_tag = '';
				
			}
			
			$this->tag .= '<'.$tag.$this->attr( $attr ).'>'.$this->value( $value ).'</'.$tag.'>';
											
			if( $this->close_tag != '' ){
				
				$this->tag .= $this->close_tag;
				
				$this->close_tag = '';
				
			}
			
		}
		
		return $this;
		
	}

	private function attr( $attr ){
		
		if( empty( $attr ) ) return;
		
		if( is_array( $attr ) ){
			
			foreach( $attr as $key => $value )
				$a[] = $key.'="'.$value.'"';
			
			return ' '.implode( ' ', $a );
		
		}

		return $attr;
		
	}
	
	private function value( $value ){
		return is_callable( $value ) ? call_user_func( $value ) : $value;
	}
	
	public function exe(){
		
		$tag = $this->tag;
		
		$this->tag = '';
		
		return $tag;
		
	}
	
}

$html = new Html();

echo $html->tag( 'html',

						$html->tag( 'body',
						
											$html->tag( 'header',
											
																	$html->tag( 'h1', 'Header' )
											
											)->tag( 'footer',
																
																$html->tag( 'h1', 'Footer' )
																
											)
						
						)

	)->exe();
 
header() è una funzione nativa di php
quando arrivi qui
PHP:
private function value( $value ){
        return is_callable( $value ) ? call_user_func( $value ) : $value;
    }
viene richiamata, ma, senza il parametro che si aspetta, di conseguenza lo script va in errore
 
non ci sono header() nello script, cmq nn ho capito bene... quando viene richiamata la funzione value non mi restituisce i parametri ???
 
Cmq non mi da nessun errore, diciamo che funziona solo che il codice html non è annidato come dovrebbe
 
Nella funzione tag() richiami la funzione value() passando come parametro la stringa "Header"
PHP:
 $html->tag( 'header',
la funzione value() verifica se la stringa passata è una function e come spiegato prima và in errore.

edit:
restituisce un warning, probabilmente li hai disabilitati
Warning: header() expects at least 1 parameter, 0 given in C:\wamp\www\Develop\html.php on line 55
 
Non so perche a me non mi visualizza nessun errore... cmq ho provato a togliere header ma il risultato non cambia mi restituisce il codice html sempre non in ordine...
 
Scusami, ma l header viene passato come tag non come valore...
PHP:
$html->tag( tag /* header */, value, attr );
 
metti degli echo nella funzione cosi vedi cosa arriva
PHP:
private function value( $value ){
        echo "valore funzione " . $value . "<br/>";
        return is_callable( $value ) ? call_user_func( $value ) : $value;
    }
 

Discussioni simili