begin process at 2008 08 20 14:57:05
1 228 884 membres
250 nouveaux aujourd'hui
14 258 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

[PHP 5.1] CLASS STRING : NOUVEL EXEMPLE SUR LA SPL


Information sur la source

Catégorie :Class et Objet ( POO ) Classé sous : itérateurs, spl, chaîne, objets, tableaux Niveau : Expert Date de création : 13/05/2008 Date de mise à jour : 14/05/2008 14:14:45 Vu : 2 353

Note :
Aucune note

Commentaire sur cette source (9)
Ajouter un commentaire et/ou une note

Description

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

  • <?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;
  • }
  • ?>
<?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;
}
?>
13 mai 2008 18:40:07 :
Petit oubli dans les commentaires
14 mai 2008 14:14:45 :
quelques modifs pour pouvoir mieux contrôler le retour des fonctions
  • signaler à un administrateur
    Commentaire de depression le 13/05/2008 22:04:59

    Bon, je vais dl ta source pour bosser avec, mais une chose me fait sourire:

    /**
    * Use natcasesort() on our string...!
    */
    $oChaine->natcasesort();


    Comment dire, c'est un peu comme ceux qui écrivent:

    // On incrémente $i:
    $i++;


    C'est pas le plus utile quoi ;)

  • signaler à un administrateur
    Commentaire de malalam le 13/05/2008 22:49:31 administrateur CS

    Hello,

    ça s'appelle de l'emphase : j'insiste simplement sur le fait que OUI, c'est une chaîne (enfin, ça y ressemble), que OUI c'est une méthode, que NON elle n'est pas définie dans ma classe, mais que OUI je vais bien l'utiliser, cette méthode non définie, qui trie un tableau, et ce sur une chaîne.
    Et ton exemple est pourtant plus utile que mon commentaire, parce que, encire faut-il savoir que ++ est un opérateur d'incrément...après tout, à qui servent les commentaires ? A ceux qui comprennent tout de suite? Naan...

  • signaler à un administrateur
    Commentaire de depression le 13/05/2008 23:39:07

    Pour le ++, tu apprends très rapidement que c'est pour de l'incrémentation. T'apprends cela environ en même temps que // ou /* */ ou encore # servent à faire des commentaires.

  • signaler à un administrateur
    Commentaire de coucou747 le 14/05/2008 02:03:10

    je passe juste comme ca :)
    rien a dire sur la classe, si ce n'est qu'elle ne s'occupe que de String, et non des types de bases, (genre les types de bases ont une representation json, ils sont serializable, on peut les passer en sql, etc...) mais c'est juste une evolution possible et pas une critique :)

    sinon, j'ai vu des langages ou certains prennaient un malin plaisir a definir /* comme un operateur couple a */ (je sais plus trop comment, mais c'etait marrant)

    bref, quand on voit du francais dans un code, peu importe ce qu'il y a devant, que ca soit #, ;, rem, ', ou //, on sait que c'est un commentaire...

  • signaler à un administrateur
    Commentaire de malalam le 14/05/2008 07:59:19 administrateur CS

    @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 :-)

  • signaler à un administrateur
    Commentaire de Teclis01 le 14/05/2008 08:58:38

    Bien le bonjour les p'tits fous ^^
    Cette classe me rappelle quelques chose >_<
    Je passerais ce soir quand j'aurais lu le code :p

  • signaler à un administrateur
    Commentaire de yoman64 le 14/05/2008 22:02:18

    Moi ça me rappele la classe de neigedhiver http://www.phpcs.com/codes/PHP5-STRING-ITERATOR_44623.aspx :-)

  • signaler à un administrateur
    Commentaire de Teclis01 le 14/05/2008 22:20:11

    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 !!!

  • signaler à un administrateur
    Commentaire de malalam le 14/05/2008 22:52:24 administrateur CS

    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... :-)

Ajouter un commentaire

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Téléchargements

Boutique

Boutique de goodies CodeS-SourceS