<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/reserved.interfaces.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'es',
  ),
  'this' => 
  array (
    0 => 'class.stringable.php',
    1 => 'Stringable',
    2 => 'La interfaz Stringable',
  ),
  'up' => 
  array (
    0 => 'reserved.interfaces.php',
    1 => 'Interfaces y clases predefinidas',
  ),
  'prev' => 
  array (
    0 => 'weakmap.offsetunset.php',
    1 => 'WeakMap::offsetUnset',
  ),
  'next' => 
  array (
    0 => 'stringable.tostring.php',
    1 => 'Stringable::__toString',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'es',
    'path' => 'language/predefined/stringable.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.stringable.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.stringable" class="reference">

 <h1 class="title">La interfaz Stringable</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 8)</p>


  <div class="section" id="stringable.intro">
   <h2 class="title">Introducción</h2>
   <p class="para">
    La interfaz <span class="interfacename"><strong class="interfacename">Stringable</strong></span> designa una clase que posee
    un método <a href="language.oop5.magic.php#object.tostring" class="link">__toString()</a>. A diferencia de la mayoría de las interfaces,
    <span class="interfacename"><strong class="interfacename">Stringable</strong></span> está implícitamente presente en cualquier clase para la cual el método mágico
    <a href="language.oop5.magic.php#object.tostring" class="link">__toString()</a> está definido, aunque puede y debe ser declarada explícitamente.
   </p>
   <p class="para">
    Su valor principal es permitir a las funciones verificar el tipo en comparación
    con el tipo de unión <code class="literal">string|Stringable</code> para aceptar ya sea un string primitivo,
    ya sea un objeto que pueda ser convertido a string.
   </p>
  </div>


  <div class="section" id="stringable.synopsis">
   <h2 class="title">Sinopsis de la Interfaz</h2>


   <div class="classsynopsis"><div class="classsynopsisinfo">
    
     <span class="modifier">interface</span> <strong class="interfacename"><strong class="interfacename">Stringable</strong></strong> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Métodos */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="stringable.tostring.php" class="methodname">__toString</a></span>(): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

   }</div>


  </div>

  <div class="section" id="stringable.examples">
   <h2 class="title">Ejemplos de Stringable</h2>
   <p class="para">
    <div class="example" id="stringable.basic-example">
     <p><strong>Ejemplo #1 Ejemplo simple</strong></p>
     <div class="example-contents"><p>Esto utiliza la <a href="language.oop5.decon.php#language.oop5.decon.constructor.promotion" class="link">promoción de propiedades del constructor</a>.</p></div>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">IPv4Address </span><span style="color: #007700">implements </span><span style="color: #0000BB">Stringable </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(<br />        private </span><span style="color: #0000BB">string $oct1</span><span style="color: #007700">,<br />        private </span><span style="color: #0000BB">string $oct2</span><span style="color: #007700">,<br />        private </span><span style="color: #0000BB">string $oct3</span><span style="color: #007700">,<br />        private </span><span style="color: #0000BB">string $oct4</span><span style="color: #007700">,<br />    ) {}<br /><br />    public function </span><span style="color: #0000BB">__toString</span><span style="color: #007700">(): </span><span style="color: #0000BB">string </span><span style="color: #007700">{<br />        return </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct1</span><span style="color: #DD0000">.</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct2</span><span style="color: #DD0000">.</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct3</span><span style="color: #DD0000">.</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">oct4</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />    }<br />}<br /><br />function </span><span style="color: #0000BB">showStuff</span><span style="color: #007700">(</span><span style="color: #0000BB">string</span><span style="color: #007700">|</span><span style="color: #0000BB">Stringable $value</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// Para un Stringable, esto llamará implícitamente a __toString().<br />    </span><span style="color: #007700">print </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$ip </span><span style="color: #007700">= new </span><span style="color: #0000BB">IPv4Address</span><span style="color: #007700">(</span><span style="color: #DD0000">'123'</span><span style="color: #007700">, </span><span style="color: #DD0000">'234'</span><span style="color: #007700">, </span><span style="color: #DD0000">'42'</span><span style="color: #007700">, </span><span style="color: #DD0000">'9'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">showStuff</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

     <div class="example-contents"><p>Resultado del ejemplo anterior es similar a:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
123.234.42.9
</pre></div>
     </div>
    </div>
   </p>
  </div>

 </div>

 





<h2>Tabla de contenidos</h2><ul class="chunklist chunklist_reference"><li><a href="stringable.tostring.php">Stringable::__toString</a> — Devuelve la representaci&oacute;n del objeto en forma de string</li></ul>
</div>
<?php manual_footer($setup); ?>