svn: /phpdoc/es/trunk/ language/oop5/iterations.xml translation.xml

From: Date: Wed, 06 Oct 2010 14:30:13 +0000
Subject: svn: /phpdoc/es/trunk/ language/oop5/iterations.xml translation.xml
Groups: php.doc.es 
Request: Send a blank email to doc-es+get-3567@lists.php.net to get a copy of this message
jpberdejo Wed, 06 Oct 2010 14:30:13 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=304159 Log: Nuevo archivo traducido Changed paths: A phpdoc/es/trunk/language/oop5/iterations.xml U phpdoc/es/trunk/translation.xml Added: phpdoc/es/trunk/language/oop5/iterations.xml =================================================================== --- phpdoc/es/trunk/language/oop5/iterations.xml (rev 0) +++ phpdoc/es/trunk/language/oop5/iterations.xml 2010-10-06 14:30:13 UTC (rev 304159) @@ -0,0 +1,271 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- $Revision$ --> +<!-- EN-Revision: 301637 Maintainer: jpberdejo Status: ready --> + <sect1 xml:id="language.oop5.iterations" xmlns="http://docbook.org/ns/docbook"> + <title>Iteración de objetos</title> + <para> + + PHP 5 ofrece una manera para que los objetos sean definidos por lo que es posible iterar + a través de una lista de elementos, con, por ejemplo, una sentencia &foreach;. Por defecto, + todas las propiedades <link linkend="language.oop5.visibility">visibles</link> serán utilizados + para la iteración. + + </para> + + <example> + <title>Iteración simple de objeto</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyClass +{ + public $var1 = 'value 1'; + public $var2 = 'value 2'; + public $var3 = 'value 3'; + + protected $protected = 'protected var'; + private $private = 'private var'; + + function iterateVisible() { + echo "MyClass::iterateVisible:\n"; + foreach($this as $key => $value) { + print "$key => $value\n"; + } + } +} + +$class = new MyClass(); + +foreach($class as $key => $value) { + print "$key => $value\n"; +} +echo "\n"; + + +$class->iterateVisible(); + +?> +]]> + </programlisting> + &example.outputs; + <screen role="php"> +<![CDATA[ +var1 => value 1 +var2 => value 2 +var3 => value 3 + +MyClass::iterateVisible: +var1 => value 1 +var2 => value 2 +var3 => value 3 +protected => protected var +private => private var +]]> + </screen> + + </example> + + <para> + Como se muestra en la salida, el &foreach; itera a través de + todas las variables <link linkend="language.oop5.visibility">visibles</link> que se puedan + acceder. Para dar un paso más allá se puede implementar una + <link linkend="language.oop5.interfaces">interfaz</link> + interna de PHP 5 llamada <varname>Iterator</varname>. Esto permite al objeto decidir qué y cómo + el objeto será iterado. + </para> + + <example> + <title>Iteración de objeto implementando Iterator</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyIterator implements Iterator +{ + private $var = array(); + private $index = 0; + + public function __construct($array) + { + if (is_array($array)) { + $this->var = $array; + } + } + + public function rewind() + { + echo "rewinding\n"; + $this->index = 0; + } + + public function current() + { + $k = array_keys($this->var); + $var = $this->var[$k[$this->index]]; + echo "current: $var\n"; + return $var; + } + + public function key() + { + $k = array_keys($this->var); + $var = $k[$this->index]; + echo "key: $var\n"; + return $var; + } + + public function next() + { + $k = array_keys($this->var); + if (isset($k[++$this->index])) { + $var = $this->var[$k[$this->index]]; + echo "next: $var\n"; + return $var; + } else { + echo "next:\n"; + return false; + } + } + + public function valid() + { + $k = array_keys($this->var); + $var = isset($k[$this->index]); + echo "valid: {$var}\n"; + return $var; + } + +} + +$values = array(1,2,3); +$it = new MyIterator($values); + +foreach ($it as $a => $b) { + print "$a: $b\n"; +} +?> +]]> + </programlisting> + &example.outputs; + <screen role="php"> +<![CDATA[ +rewinding +valid: 1 +current: 1 +key: 0 +0: 1 +next: 2 +valid: 1 +current: 2 +key: 1 +1: 2 +next: 3 +valid: 1 +current: 3 +key: 2 +2: 3 +next: (no more) +valid: +]]> + </screen> + + </example> + + <para> + También se puede definir la clase de modo que no tiene que definir + todas las funciones de <varname>Iterator</varname>, simplemente implementando + la interfaz de PHP 5 <varname>IteratorAggregate</varname>. + </para> + + <example> + <title>Iteración de objeto implementando IteratorAggregate</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyCollection implements IteratorAggregate +{ + private $items = array(); + private $count = 0; + + // Required definition of interface IteratorAggregate + public function getIterator() { + return new MyIterator($this->items); + } + + public function add($value) { + $this->items[$this->count++] = $value; + } +} + +$coll = new MyCollection(); +$coll->add('value 1'); +$coll->add('value 2'); +$coll->add('value 3'); + +foreach ($coll as $key => $val) { + echo "key/value: [$key -> $val]\n\n"; +} +?> +]]> + </programlisting> + &example.outputs; + <screen role="php"> +<![CDATA[ +rewinding +current: value 1 +valid: 1 +current: value 1 +key: 0 +key/value: [0 -> value 1] + +next: value 2 +current: value 2 +valid: 1 +current: value 2 +key: 1 +key/value: [1 -> value 2] + +next: value 3 +current: value 3 +valid: 1 +current: value 3 +key: 2 +key/value: [2 -> value 3] + +next: +current: +valid: +]]> + </screen> + + </example> + + <note> + <para> + Para más ejemplos de iteradores, vea la + <link linkend="spl.iterators">extensión SPL</link>. + </para> + </note> + + </sect1> + +<!-- Keep this comment at the end of the file +Local variables: +mode: sgml +sgml-omittag:t +sgml-shorttag:t +sgml-minimize-attributes:nil +sgml-always-quote-attributes:t +sgml-indent-step:1 +sgml-indent-data:t +indent-tabs-mode:nil +sgml-parent-document:nil +sgml-default-dtd-file:"~/.phpdoc/manual.ced" +sgml-exposed-tags:nil +sgml-local-catalogs:nil +sgml-local-ecat-files:nil +End: +vim600: syn=xml fen fdm=syntax fdl=2 si +vim: et tw=78 syn=sgml +vi: ts=1 sw=1 +--> + Property changes on: phpdoc/es/trunk/language/oop5/iterations.xml ___________________________________________________________________ Added: svn:keywords + Id Rev Revision Date LastChangedDate LastChangedRevision Author LastChangedBy HeadURL URL Added: svn:eol-style + native Modified: phpdoc/es/trunk/translation.xml =================================================================== --- phpdoc/es/trunk/translation.xml 2010-10-06 14:07:47 UTC (rev 304158) +++ phpdoc/es/trunk/translation.xml 2010-10-06 14:30:13 UTC (rev 304159) @@ -56,7 +56,6 @@ <file name="/reference/printer/*" person="jesusruiz" type="working" date="05.09.2010"/> <file name="/reference/xml/*" person="jesus_cova" type="working" date="14.09.2010"/> <file name="reference/oauth/*" person="agarzon" type="working" date="17.09.2010"/> - <file name="/languaje/context/*" person="jpberdejo" type="working" date="01.10.2010"/> </work-in-progress> </translation>

« previous php.doc.es (#3567) next »