note 57854 deleted from function.ob-start by cmb
| From: | cmb@php.net | Date: | Sun, 14 Jul 2019 11:26:34 +0000 |
| Subject: | note 57854 deleted from function.ob-start by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211574@lists.php.net to get a copy of this message | ||
Note Submitter: cyrille.berliat[no spam]free.fr
----
If you're trying to use ob_start() in some PHP5 classes (probably works on PHP4 classes), this
is the good way :
<?
class HTMLPage
{
//----------------------------------------------------------------- PUBLIC
//----------------------------------------------------- Méthodes publiques
public static function ConvertIntoSGML( $source )
// Mode d'emploi :
//convertit une string en une SGML valide
//
// Renvoie :
//la chaine traitée
//
// Algorithme :
//analyse char par char de la chaine. Si un caractère est de nombre ASCII > 127,
//conversion en son code SGML.
{
$newString = '';
for( $i = 0; $i < strlen( $source ) ; $i++ ) {
$o = ord( $source{ $i } );
$newString .= ( ( $o > 127 ) ? '&#'.$o.';': $source{ $i } );
}
return $newString;
}
public function FlushSite( $source )
{
return $this->ConvertIntoSGML ( $source );
}
//-------------------------------------------- Constructeurs - destructeur
function __construct()
// Mode d'emploi (constructeur) :
//initialise la buffurisation
//
// Contrat :
//
{
ob_start( array ( & $this , 'FlushSite' ) );
} //---- Fin du constructeur
//------------------------------------------------------ Méthodes Magiques
//------------------------------------------------------------------ PRIVE
}
// Example :
$webdesign = new HTMLPage ( );
echo 'Héllo world'; // Will produce the source 'héllo world'
?>
Without the & before $this, you'll loose your content because ob_start() will call the
flushsite() function from a clone of the object and not the caller object himself.
Note : call_back function must be public because ob_start() is in an extern scope from your class :)
I hope this will help you!