mirror of
https://github.com/php/doc-es.git
synced 2026-04-24 15:48:13 +02:00
9c4337cbf2
git-svn-id: https://svn.php.net/repository/phpdoc/es/trunk@338573 c90b9560-bf6c-de11-be94-00142212c4b1
152 lines
3.1 KiB
XML
152 lines
3.1 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: b7a780f8e762e290552b9f79d346f2bcbc272e0c Maintainer: seros Status: ready -->
|
|
<!-- Reviewed: no -->
|
|
|
|
<sect1 xml:id="language.oop5.anonymous" xmlns="http://docbook.org/ns/docbook">
|
|
<title>Clases anónimas</title>
|
|
|
|
<para>
|
|
Se ha añadido soporte para clases anónimas en PHP 7. Las clases anónimas son
|
|
útiles cuando es necesario crear objetos sencillos y únicos.
|
|
</para>
|
|
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
// Código anterior a PHP 7
|
|
class Logger
|
|
{
|
|
public function log($msg)
|
|
{
|
|
echo $msg;
|
|
}
|
|
}
|
|
|
|
$util->setLogger(new Logger());
|
|
|
|
// Código de PHP 7+
|
|
$util->setLogger(new class {
|
|
public function log($msg)
|
|
{
|
|
echo $msg;
|
|
}
|
|
});
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
|
|
<para>
|
|
Pueden pasar argumentos a través de sus constructores, extender otras clases,
|
|
implementar interfaces, y utilizar rasgos al igual que una clase normal:
|
|
</para>
|
|
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class SomeClass {}
|
|
interface SomeInterface {}
|
|
trait SomeTrait {}
|
|
|
|
var_dump(new class(10) extends SomeClass implements SomeInterface {
|
|
private $num;
|
|
|
|
public function __construct($num)
|
|
{
|
|
$this->num = $num;
|
|
}
|
|
|
|
use SomeTrait;
|
|
});
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
object(class@anonymous)#1 (1) {
|
|
["Command line code0x104c5b612":"class@anonymous":private]=>
|
|
int(10)
|
|
}
|
|
]]>
|
|
</screen>
|
|
</informalexample>
|
|
|
|
<para>
|
|
El anidamiento de una clase anónima dentro de otra clase no le proporciona acceso a
|
|
ningún método o propiedad privados o protegidos de la clase externa. Para
|
|
utilizar las propiedades o métodos protegidos de la clase externa, la clase anónima
|
|
puede extender la clase externa. Para utilizar las propiedades o métodos protegidos de
|
|
la clase externa en al clase anónima, estos deben pasarse a su
|
|
constructor:
|
|
</para>
|
|
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class Externa
|
|
{
|
|
private $prop = 1;
|
|
protected $prop2 = 2;
|
|
|
|
protected function func1()
|
|
{
|
|
return 3;
|
|
}
|
|
|
|
public function func2()
|
|
{
|
|
return new class($this->prop) extends Externa {
|
|
private $prop3;
|
|
|
|
public function __construct($prop)
|
|
{
|
|
$this->prop3 = $prop;
|
|
}
|
|
|
|
public function func3()
|
|
{
|
|
return $this->prop2 + $this->prop3 + $this->func1();
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
echo (new Externa)->func2()->func3();
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
6
|
|
]]>
|
|
</screen>
|
|
</informalexample>
|
|
</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
|
|
-->
|