mirror of
https://github.com/php/doc-tr.git
synced 2026-03-23 23:02:09 +01:00
200 lines
4.0 KiB
XML
200 lines
4.0 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: a9edd62d087ab1eb6292c795b7256e14ff9f1234 Maintainer: nilgun Status: ready -->
|
||
<sect1 xml:id="language.oop5.anonymous" xmlns="http://docbook.org/ns/docbook">
|
||
<title>Anonim Sınıflar</title>
|
||
|
||
<para>
|
||
Anonim sınıflar basit, tek
|
||
seferlik nesnelerin oluşturulması gerektiğinde kullanışlıdır.
|
||
</para>
|
||
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
// Örtük sınıf kullanarak
|
||
class Logger
|
||
{
|
||
public function log($msg)
|
||
{
|
||
echo $msg;
|
||
}
|
||
}
|
||
|
||
$util->setLogger(new Logger());
|
||
|
||
// Anonim sınıf kullanarak
|
||
$util->setLogger(new class {
|
||
public function log($msg)
|
||
{
|
||
echo $msg;
|
||
}
|
||
});
|
||
]]>
|
||
</programlisting>
|
||
</informalexample>
|
||
|
||
<para>
|
||
Bağımsız değişkenlerini kurucularına iletebilir, diğer sınıfları genişletebilir,
|
||
arayüzleri gerçekleyebilir ve normal bir sınıf gibi nitelikleri
|
||
kullanabilirler:
|
||
</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>
|
||
Anonim bir sınıfı başka bir sınıfta yerleştirmek, herhangi bir özel veya
|
||
korunan yönteme veya bu dış sınıfın özelliklerine erişim sağlamaz. Dış
|
||
sınıfın korumalı özelliklerini veya yöntemlerini kullanabilmek için, anonim
|
||
sınıf dış sınıfı genişletebilir. Dış sınıfın anonim sınıftaki özel
|
||
özelliklerini kullanmak için kurucusundan geçirilmesi gerekir:
|
||
</para>
|
||
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
class Outer
|
||
{
|
||
private $prop = 1;
|
||
protected $prop2 = 2;
|
||
|
||
protected function func1()
|
||
{
|
||
return 3;
|
||
}
|
||
|
||
public function func2()
|
||
{
|
||
return new class($this->prop) extends Outer {
|
||
private $prop3;
|
||
|
||
public function __construct($prop)
|
||
{
|
||
$this->prop3 = $prop;
|
||
}
|
||
|
||
public function func3()
|
||
{
|
||
return $this->prop2 + $this->prop3 + $this->func1();
|
||
}
|
||
};
|
||
}
|
||
}
|
||
|
||
echo (new Outer)->func2()->func3();
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
6
|
||
]]>
|
||
</screen>
|
||
</informalexample>
|
||
|
||
<para>
|
||
Aynı anonim sınıf bildirimiyle oluşturulan tüm nesneler, bu sınıfın
|
||
örnekleridir.
|
||
</para>
|
||
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
function anonymous_class()
|
||
{
|
||
return new class {};
|
||
}
|
||
|
||
if (get_class(anonymous_class()) === get_class(anonymous_class())) {
|
||
echo 'aynı sınıf';
|
||
} else {
|
||
echo 'farklı sınıf';
|
||
}]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
aynı sınıf
|
||
]]>
|
||
</screen>
|
||
</informalexample>
|
||
|
||
<note>
|
||
<para>
|
||
Aşağıdaki örnekte gösterildiği gibi, anonim sınıflara motor tarafından
|
||
bir ad verildiğini unutmayın. Bu isim, güvenilmemesi gereken bir
|
||
gerçekleme ayrıntısı olarak görülmelidir.
|
||
</para>
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
echo get_class(new class {});
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.similar;
|
||
<screen>
|
||
<![CDATA[
|
||
class@anonymous/in/oNi1A0x7f8636ad2021
|
||
]]>
|
||
</screen>
|
||
</informalexample>
|
||
</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
|
||
-->
|