Struppi: sprintf ein array übergeben.

Beitrag lesen

ich versuch mich heute ein bisschen an PHP und bin jetzt auf ein Problem gestossen dass ich nicht lösen kann.

Ich zeig mal einfach was ich machen will.

  
class Texte {  
 var $msg = array(  
 'text1' => 'Der Text: %s',  
 'text2' => 'Der Text %s hat %s Zeichen.'  
 );  
  
 function _() {  
  $args = func_get_args();  
  $what = array_shift($args);  
  return sprintf($this->msg[$what], $args);  
 }  
}  
  
$t = new Texte();  
$string = 'Hallo Welt!';  
echo $t->_('text1', $string);  
echo $t->_('text2', $string, strlen($string));  

Das geht natürlich nicht, macht aber eventuell deutlich was ich möchte.

In Perl würde die Funktion so aussehen:

sub _() {  
my $this = shift;  
my $args = shift;  
return sprintf($this->{msg}->{$what}, @_);  
}  

Aber wie mache ich das mit PHP?

Struppi.