[php 5.1] class string : nouvel exemple sur la spl

Soyez le premier à donner votre avis sur cette source.

Snippet vu 8 672 fois - Téléchargée 19 fois

Contenu du snippet

Cette classe a été écrite essentiellement pour montrer que l'on peut très facilement écrire ne classe pratique et amusante grâce à la SPL (http://www.php.net/~helly/php/ext/spl/main.html).
Elle permet d'utiliser les fonctions de tableaux, ou les méthodes de la classe ArrayIterator sur une chaîne.

Source / Exemple :


<?php
/**

  • Basic error handler to convert php errors into php exceptions. Best use a more complete one...
*
  • @param int $errno
  • @param string $errstr
  • @param string $errfile
  • @param int $errline
  • /
function ErrorToException($errno, $errstr, $errfile, $errline) { throw new Exception($errstr, $errno); } /**
  • Setting our error handler
  • /
set_error_handler('ErrorToException'); /**
  • class string
  • @author : Johan Barbier <barbier_johan@hotmail.com>
  • @version : 20080513
  • @desc : small string class which allows to use strings as arrays. This class was mainly written to show how the SPL coult be used easily to make funny stuff!
*
  • /
class string implements Iterator, Countable, SeekableIterator { /**
  • Parameter in which the string will be stored, as an ArrayIterator object
*
  • @var ArrayIterator
  • /
private $aChaine; /**
  • Constants defined to se the return by reference or not trick in the string::__call() method
*
  • /
const RETURNS_REFERENCE_USED = true; const RETURNS_REFERENCE_NOT_USED = false; /**
  • Constructor. Needs a string as first and only parameter
*
  • @param string $sChaine
  • /
public function __construct($sChaine) { if(!is_string($sChaine)) { throw new InvalidArgumentException('Parameter must be a string'); } $this->aChaine = new ArrayIterator(str_split($sChaine)); } /**
  • Here is the magic : __call will allow to call any methods of the ArrayIterator class, or any function...these should be array functions, or there might be an error
*
  • @param string $sFunction : function/method to be called
  • @param array $aArgs : array of arguments for the function/method to be called.
  • @return mixed
  • /
public function __call($sFunction, $aArgs) { /**
  • Case we call an ArrayIterator method
  • /
if(method_exists($this->aChaine, $sFunction)) { return call_user_func_array(array($this->aChaine, $sFunction), $aArgs); /**
  • Case we try to call a basic function
  • /
} elseif(function_exists($sFunction)) { /**
  • Try to find the firs array parameter of the function asked for, so that we could replace it with our ArrayIterator
  • IF the first argument is a boolean, it is used as the $bReturnsByReference variable; it means that IF the function called returns an array, then our string object will be replaced by this return. The exemple shows how it works with the array_reverse() function.
  • /
if(!isset($aArgs[0]) || !is_bool($aArgs[0])) { $bReturnsByReference = string::RETURNS_REFERENCE_NOT_USED; } else { $bReturnsByReference = $aArgs[0]; array_shift($aArgs); } $oFuncRef = new reflectionFunction($sFunction); $iKeepPos = null; foreach($oFuncRef->getParameters() as $iPos => $oParamRef) { if($oParamRef->isArray()) { $iKeepPos = $iPos; break; } } if(!is_null($iKeepPos)) { $aKeepArgs = $aArgs; $iCpt = 0; foreach($aKeepArgs as $iK => $mVal) { if($iK === $iKeepPos) { $aArgs[$iCpt] = $this->aChaine->getArrayCopy(); ++$iCpt; } else { $aArgs[$iCpt] = $mVal; } ++$iCpt; } } else { /**
  • If we could not find any array in the arguments of the function (ParameterReflection::isArray() does not work very well with old functions...too bad...) we force it assuming that the array is the first argument. If not...well, there will be an error!
  • /
array_unshift($aArgs, $this->aChaine->getArrayCopy()); } $mReturn = call_user_func_array($sFunction, $aArgs); if(is_array($mReturn) && string::RETURNS_REFERENCE_USED === $bReturnsByReference) { $this->aChaine = new ArrayIterator($mReturn); } return $mReturn; } throw new BadMethodCallException($sFunction.' does not exist'); } /**
  • Returns the string
*
  • @return string
  • /
public function __toString() { $s = ''; $this->rewind(); while($this->valid()) { $s .= $this->current(); $this->next(); } $this->rewind(); return $s; } public function valid() { return $this->aChaine->valid(); } public function current() { return $this->aChaine->current(); } public function next() { $this->aChaine->next(); } public function key() { return $this->aChaine->key(); } public function rewind() { $this->aChaine->rewind(); } public function count() { return $this->aChaine->count(); } public function seek($offset) { $this->aChaine->seek($offset); } } try { /**
  • Instanciation
  • /
$sChaine = 'Hello World!!'; $oChaine = new string($sChaine); /**
  • basic loop on each character of our string
  • /
foreach($oChaine as $iK => $sV) { echo $iK, ' => ', $sV, "\n"; } /**
  • Use natcasesort() on our string...!
  • Here, we do not need to use the $bReturnsByReference boolean flag because the natcasesort() used here is NOT the function, but the ArrayIterator method.
  • /
$oChaine->natcasesort(); echo $oChaine, "\n"; /*
  • Reverse our string...!
  • We give the function a parameter, a boolean true, because we want the return of array_reverse() to be used as our new string object.
  • /
$oChaine->array_reverse(string::RETURNS_REFERENCE_USED); echo $oChaine, "\n"; /**
  • Count occurences of each unique character in our string. As this function returns an array, we explicitely ask that this array won't be used to replace our current string object (if we had not told anything, it would not have done so anyway)
  • /
print_r($oChaine->array_count_values(string::RETURNS_REFERENCE_NOT_USED)); echo $oChaine, "\n"; /**
  • Here, we deliberately call an invalid function, it will be caught by the basic error handler
  • /
echo $oChaine->floor(2); } catch(Exception $e) { echo $e; } ?>

A voir également

Ajouter un commentaire Commentaires
malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 25
14 mai 2008 à 22:52
Hi,

c'est très exactement la même base que la classe de Neige. Sauf que l'idée n'est pas la même; le but, si : montrer la SPL en action. L'idée, non...moi, j'avais envie de m'amuser à utiliser les fonctions de tableau de PHP (puissantes et pratiques) sur des chaînes. Ca c'est l'excuse :-) Le truc que je voulais vraiment montrer, c'est d'une part la SPL (là, c'est exactement comme la classe de Neige), et une utilisation possible de la méthode __call(). On voit la même utilisation dans certains exemples de la SPL PHP (CacheIterator par exemple).
Ce n'est pas parfait, c'est juste un exemple, c'est purement didactique. Mais je pense qu'en fouillant on peut arriver à un truc extrêmement puissant. Là ça marche déjà pas mal mais c'est du bricolage (la constante à passer pour faire un retour par référence, etc...).
Bref l'idée est juste de montrer encore une fois qu'on peut s'amuser avec la SPL... :-)
Teclis01 Messages postés 1423 Date d'inscription mardi 14 décembre 2004 Statut Membre Dernière intervention 29 décembre 2012 4
14 mai 2008 à 22:20
C'est propre, simple et explicite.
Effectivement ça se ressemble mais... j'aime les notations plus typées :p
malalam!!! lâche la librairie spl lâcheuuuu !!!
cs_yoman64 Messages postés 592 Date d'inscription samedi 19 janvier 2002 Statut Membre Dernière intervention 4 décembre 2008
14 mai 2008 à 22:02
Moi ça me rappele la classe de neigedhiver http://www.phpcs.com/codes/PHP5-STRING-ITERATOR_44623.aspx :-)
Teclis01 Messages postés 1423 Date d'inscription mardi 14 décembre 2004 Statut Membre Dernière intervention 29 décembre 2012 4
14 mai 2008 à 08:58
Bien le bonjour les p'tits fous ^^
Cette classe me rappelle quelques chose >_<
Je passerais ce soir quand j'aurais lu le code :p
malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 25
14 mai 2008 à 07:59
@depression : oui...encore que, je suis persuadé que c'est pas le cas pour tout le monde...et tu l'apprends comment ? Par exemple, parce tu auras vu un commentaire du type "On incrémente notre variable : $a++;" (ou ++$a). Que ce soit dans un bouquin, la doc, un cours, un tuto, un code, et quelle que soit la forme.
Mon point de vue est qu'il n'y a jamais trop de commentaires. D'ailleurs sur ce code j'ai pas terminé, je rajouterai les suivants plus tard. les commentaires aident à suivre le cours du code.

@coucou : voui, de toute manière cette classe n'a pas pour vocation d'être une classe complète de typage. Même si le sujet est intéressant et que je ferai peut-être évoluer le bin's dans ce sens un jour. Et ça peut-être amusant de compléter celle-là voir jusqu'où on peut aller, en effet :-)

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.