<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Geliştirici &#187; PHP</title>
	<atom:link href="http://www.webgelistirici.com/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.webgelistirici.com</link>
	<description>:: PHP, Kohana, JavaScript, Ajax, XHTML, XML, CSS, MySQL ve diğer web yazılım geliştirme teknolojileri hakkında faydalı bilgi ve teknikler</description>
	<lastBuildDate>Tue, 03 Jan 2012 13:55:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-11</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-11.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-11.html#comments</comments>
		<pubDate>Mon, 16 May 2011 12:05:16 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[aşırı yükleme]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[nesne]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[overloading]]></category>
		<category><![CDATA[sınıf]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=559</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>Nesne Özelliklerinin Aşırı Yüklenmesi (Property Overloading)</h2>
<p>PHP&#8217;de nesne özelliklerinin aşırı yüklenmesi, bir nesnenin olmayan bir özelliğinin çalışma zamanında dinamik olarak oluşturulmasını ifade eder.</p>
<p>Olmayan özelliklerin çalışma esnasında oluşturulabilmesi için <code>__set()</code>, <code>__get()</code>, <code>__isset()</code> ve <code>__unset()</code> sihirli yöntemlerinden faydalanılır.</p>
<p><span id="more-559"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Olmayan özellikleri çalışma esnasında dizi tipindeki bu özellikte tutacağız</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$dinamik_ozellikler</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ozellik</span><span style="color: #339933;">,</span> <span style="color: #000088;">$de</span>ğer<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Olmayan bir özelliğe değer atanmaya çalışılırsa bu yöntem devreye girer</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dinamik_ozellikler</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ozellik</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$de</span>ğer<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ozellik</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Olmayan bir özelliğin değeri alınmaya çalışılırsa bu yöntem devreye girer</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dinamik_ozellikler</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ozellik</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dinamik_ozellikler</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ozellik</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __isset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ozellik</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Olmayan bir özelliğin varlığı kontrol edilirse bu yöntem devreye girer</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dinamik_ozellikler</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ozellik</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __unset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ozellik</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Olmayan bir özellik yokedilmeye çalışılırsa bu yöntem devreye girer</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dinamik_ozellikler</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ozellik</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dinamik_ozellikler</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ozellik</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// __set() devreye girer</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">x</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// __get() devreye girer</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">x</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// __isset() devreye girer</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// __unset() devreye girer</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-11.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-10</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-10.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-10.html#comments</comments>
		<pubDate>Fri, 13 May 2011 11:27:13 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[implements]]></category>
		<category><![CDATA[isim alanı]]></category>
		<category><![CDATA[namespace]]></category>
		<category><![CDATA[nesne]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[sınıf]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=529</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>İsim Alanı (Namespace)</h2>
<p>Özellikle büyük projelerde çok sayıda sınıf ve kütüphanenin birlikte kullanılması sonucu isim çakışmalarının olma ihtimali ortaya çıkar. Bunun önüne geçmek için genellikle framework yapılarında çok uzun sınıf isimleri verilir.</p>
<p>PHP 5.3 ile beraber bu tür durumlar göz önünde bulundurularak isim alanı (<em>namespace</em>) olanağı geldi. İsim alanı sayesinde aynı isimde sınıflar kullanabiliriz. Bir isim alanı içindeki sınıfı çağırırken bilgisayardaki dosya sistemindeki mantığa benzer şekilde &#8220;<code>alan\sinif</code>&#8221; biçiminde kullanılır. Görüldüğü gibi ayırıcı karakter olarak &#8220;<code>\</code>&#8221; karakteri kullanılmaktadır.</p>
<p><span id="more-529"></span></p>
<p>İsim alanı tanımlamak için <code>namespace</code> deyiminden faydalanılır. İsim alanının tanımlandığı dosyada, namespace bloğunun dışında başka bir PHP kodu bulunamaz, aksi halde ölümcül hata (<em>fatal error</em>) oluşacaktır.</p>
<p>Bir dosya içinde birden fazla isim alanı aynı anda tanımlanabilir. Benzer şekilde bir isim alanı ile ilgili sınıflar ayrı dosyalarda da tanımlanabilir. Yani birden fazla dosyada aynı isim alanı ile ilgili tanımlamalar yapmak mümkündür.</p>
<p>Bir isim alanına &#8220;<code>use</code>&#8221; deyimi ile, başka bir isim alanını veya başka bir isim alanında tanımlı bir sınıfı dahil edebiliriz. Ayrıca istersek dahil ettiğimiz isim alanı veya sınıfa &#8220;<code>as</code>&#8221; deyimi ile takma ad verebiliriz.</p>
<p><strong>Harici dosya:</strong> <code>isim_alani.php</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// namespace bloğu dışında PHP kodu bulunamaz</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">namespace</span> alan1 <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
       <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#123;</span>
               <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'İsim alanı : '</span><span style="color: #339933;">.</span>__NAMESPACE__<span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #339933;">;</span>
               <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Sınıf : '</span><span style="color: #339933;">.</span><span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #339933;">;</span>
               <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Yöntem : '</span><span style="color: #339933;">.</span><span style="color: #009900; font-weight: bold;">__METHOD__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;&lt;br/&gt;'</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// namespace bloğu dışında PHP kodu bulunamaz</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">namespace</span> alan2 <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Durum 1 : İsim alanına başka bir isim alanını dahil ediyoruz</span>
    <span style="color: #000000; font-weight: bold;">use</span> alan1<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// (Durum-1)</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Durum 2 : İsim alanına başka bir isim alanındaki bir sınıfı dahil ediyoruz</span>
    <span style="color: #666666; font-style: italic;">//use alan1\sinif2; // (Durum-2)</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Durum 3 : İsim alanına dahil ettiğimiz sınıflara takma ad tanımlayabiliyoruz</span>
    <span style="color: #666666; font-style: italic;">//use alan1\sinif2 as _sinif2; // (Durum-3)</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
       <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#123;</span>
           <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'İsim alanı : '</span><span style="color: #339933;">.</span>__NAMESPACE__<span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #339933;">;</span>
           <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Sınıf : '</span><span style="color: #339933;">.</span><span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #339933;">;</span>
           <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Yöntem : '</span><span style="color: #339933;">.</span><span style="color: #009900; font-weight: bold;">__METHOD__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;&lt;br/&gt;'</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
&nbsp;
       <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#123;</span>
           <span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> alan1\sinif2<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// (Durum-1)</span>
&nbsp;
           <span style="color: #666666; font-style: italic;">//$nesne = new sinif2; // (Durum-2)</span>
&nbsp;
           <span style="color: #666666; font-style: italic;">//$nesne = new _sinif2; // (Durum-3)</span>
&nbsp;
           <span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// namespace bloğu dışında PHP kodu bulunamaz</span></pre></div></div>

<p>Bu harici dosyayı kullanarak örneğimizi yapalım.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'isim_alani.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$nesne1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> alan1\sinif<span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> alan2\sinif<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$nesne1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
Çıktı:
&nbsp;
İsim alanı : alan1
Sınıf : alan1\sinif
Yöntem : alan1\sinif::yontem
*/</span>
&nbsp;
<span style="color: #000088;">$nesne2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
Çıktı:
&nbsp;
İsim alanı : alan2
Sınıf : alan2\sinif
Yöntem : alan2\sinif::yontem
*/</span>
&nbsp;
<span style="color: #000088;">$nesne2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
Çıktı:
&nbsp;
İsim alanı : alan1
Sınıf : alan1\sinif2
Yöntem : alan1\sinif2::yontem
*/</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-10.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-9</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-9.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-9.html#comments</comments>
		<pubDate>Fri, 13 May 2011 11:11:45 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[arayüz]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[nesne]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[sınıf]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=526</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>Arayüz (Interface)</h2>
<p>Arayüz, bir sınıfta bulunması gereken yöntemlerin tanımlandığı bir yapıdır. Arayüzde sadece hangi yöntemlerin bulunacağı belirtilir ancak yöntemlerin nasıl gerçekleştirileceği belirtilmez. Yani yöntemlerin sadece tanımı yapılır, gövdeleri yazılmaz. Yöntemlerin gövdeleri arayüzü kullanan sınıflar içinden yapılır. Böylece bir arayüzü kullanan her sınıf, arayüzdeki yöntemlerin farklı şekilde iş yapmasını sağlayabilir.</p>
<p>Arayüzdeki tüm yöntemlerin <code>public</code> olarak tanımlanması gerekmektedir. Arayüzde tanımlanmış olan tüm yöntemlerin, o arayüzü kullanan sınıfın içinde gövdelerinin tanımlanması zorunludur. Aksi halde ölümcül hata (<em>fatal error</em>) verilecektir.</p>
<p>Bir sınıfın bir arayüzü kullanabilmesi için &#8220;<code>implements</code>&#8221; deyiminden faydalanılır. Aralarına virgül konarak bir sınıfın aynı anda birden fazla arayüzü kullanmasını sağlayabiliriz.</p>
<p><span id="more-526"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> arayuz1 <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Public olmak zorunda</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> arayuz2 <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Public olmak zorunda</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> sinif implements arayuz1<span style="color: #339933;">,</span> arayuz2 <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Tanımlanmak zorunda, yoksa hata verir</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Selam '</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Tanımlanmak zorunda, yoksa hata verir</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Merhaba '</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Dünya'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Çıktı: Selam Merhaba Dünya</span></pre></div></div>

<p>Şimdi de başka bir örnek yapalım. Örneğin veritabanı nesnemizi türetmek için kullandığımız 2 tane sınıfımız olsun. Biri MySQL ile veritabanı işlemlerini yapmamızı sağlayan <code>vt_mysql</code> sınıfı, diğeri de PostgreSQL ile yapmamızı sağlayan <code>vt_pgsql</code> sınıfı olsun. Her iki sınıfta da aynı yöntemler tanımlı olmasını zorunlu kılmak için <code>I_vt</code> adında bir arayüz kullanalım. Böylece koda müdahale etmeden sadece veritabanı işlemlerinde kullandığım nesnenin türetildiği sınıfı değiştirerek 2 farklı VTYS kullanabileceğiz. Çünkü her iki durumda da veritabanı nesnesi aynı isimde yöntemlere sahip olacak ve bu yöntemler aynı parametrelerle aynı sonuçları üretecek. Sadece gövde kısımlarında işin yapılma şekli farklı olacak. Birinde MySQL&#8217;in komutları ile diğerinde PostgreSQL&#8217;in komutları ile işlemler yapılacak.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> I_vt <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> baglan<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sunucu</span><span style="color: #339933;">,</span> <span style="color: #000088;">$kullanici_adi</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sifre</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> veritabani_sec<span style="color: #009900;">&#40;</span><span style="color: #000088;">$veritabani_adi</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sorgu_calistir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorgu</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> kayitlari_getir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vt_kaynak</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// $vt_kaynak : mysql_query() fonksiyonundan dönen değer</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> baglantiyi_sonlandir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vt_baglanti</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// $vt_baglanti : mysql_connect() fonksiyonundan dönen değer</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> vt_mysql implements I_vt <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> baglan<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sunucu</span><span style="color: #339933;">,</span> <span style="color: #000088;">$kullanici_adi</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sifre</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> veritabani_sec<span style="color: #009900;">&#40;</span><span style="color: #000088;">$veritabani_adi</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sorgu_calistir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorgu</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> kayitlari_getir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vt_kaynak</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> baglantiyi_sonlandir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vt_baglanti</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> vt_pgsql implements I_vt <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> baglan<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sunucu</span><span style="color: #339933;">,</span> <span style="color: #000088;">$kullanici_adi</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sifre</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> veritabani_sec<span style="color: #009900;">&#40;</span><span style="color: #000088;">$veritabani_adi</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sorgu_calistir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorgu</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> kayitlari_getir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vt_kaynak</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> baglantiyi_sonlandir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vt_baglanti</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Veritabanı işlemlerini MySQL ile yapmak istersem</span>
<span style="color: #000088;">$vt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> vt_mysql<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Veritabanı işlemlerini PostgreSQL ile yapmak istersem</span>
<span style="color: #000088;">$vt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> vt_pgsql<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Her iki durumda da aşağıdaki yöntemler geçerli olacaktır</span>
<span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">baglan</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sunucu</span><span style="color: #339933;">,</span> <span style="color: #000088;">$kullanici_adi</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sifre</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">veritabani_sec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$veritabani_adi</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sorgu_calistir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorgu</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$kayitlar</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">kayitlari_getir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vt_kaynak</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">baglantiyi_sonlandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vt_baglanti</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-9.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-8</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-8.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-8.html#comments</comments>
		<pubDate>Fri, 13 May 2011 11:00:21 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[abstract]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[sınıf]]></category>
		<category><![CDATA[soyut]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=520</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>Soyut Sınıf (Abstract Class)</h2>
<p>Soyut sınıf, her koşulda geçerli olabilecek genel özellik ve yöntemleri tanımladığımız bir sınıf şablonudur. Soyut sınıf kullanılarak sadece sınıf kalıtılabilir, nesne türetilmez. Bir soyut sınıftan nesne türetilmeye çalışılırsa hata oluşucaktır.</p>
<p>Soyut sınıflar içinde soyut yöntemler tanımlanabilir. Bu durumda soyut yöntemin sadece tanım satırı yazılır, gövde bloğu yazılmaz. Soyut yöntemin detayları türetilen sınıfta yazılır. Soyut yöntemlerin türetilen sınıfta gövdelerinin tanımlanması zorunludur. Yazılmazsa hata oluşacaktır.</p>
<p><span id="more-520"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> soyut_sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Selam '</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> abstract <span style="color: #000000; font-weight: bold;">function</span> yontem1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Soyut yöntem; sadece tanım satırı var, gövdesi yok</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Merhaba '</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #000000; font-weight: bold;">extends</span> soyut_sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Tanımlanmak zorunda, yoksa hata verir</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">x</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Dünya'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// $nesne = new soyut_sinif; // HATA; soyut sınıftan nesne türetilemez</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Çıktı: Selam Merhaba Dünya</span></pre></div></div>

<p>Şimdi de gerçek hayattan bir örnek verelim:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> canli <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Bu sınıf, bütün canlılara özgü en genel özellik ve eylemleri içeriyor</span>
    <span style="color: #666666; font-style: italic;">// &quot;canli&quot; sınıfı tek başına bir anlam ifade etmiyor</span>
    <span style="color: #666666; font-style: italic;">// Kendisinden başka sınıflar türetildiğinde anlam ifade ediyor</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$yas</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Her canlının bir yaşı vardır</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$yasama_durumu</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Her canlı bir gün ölür</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">// Her canlı beslenir ancak beslenme şekilleri birbirinden farklıdır</span>
    <span style="color: #000000; font-weight: bold;">public</span> abstract <span style="color: #000000; font-weight: bold;">function</span> beslen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> hayvan <span style="color: #000000; font-weight: bold;">extends</span> canli <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$tur</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$cins</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> beslen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Hayvanın beslenmesi insandan farklıdır</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> insan <span style="color: #000000; font-weight: bold;">extends</span> canli <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$cinsiyet</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$meslek</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> beslen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// İnsanın beslenmesi hayvandan farklıdır</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-8.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-7</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-7.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-7.html#comments</comments>
		<pubDate>Tue, 10 May 2011 10:08:11 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[sınıf]]></category>
		<category><![CDATA[veritabanı]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=439</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>Veritabanı İşlemleri İçin Basit Bir Sınıf Yazalım</h2>
<p>Temel mantığı anlatabilmek adına, filtreleme ve güvenlik önlemlerini gözardı ederek temel veritabanı işlemlerinin yapılabileceği son derece sade bir sınıf yazalım. Temel anlaşıldıktan sonra bu sınıf daha da geliştirilebilir.</p>
<p>&#8220;<code><strong>test</strong></code>&#8221; isimli bir vertabanımız ve bu veritabanı içinde &#8220;<code><strong>kisi</strong></code>&#8221; adında bir tablomuz olsun. Bu tablonun içeriği aşağıdaki gibi olsun;</p>
<div style="text-align:center;"><a href="http://www.webgelistirici.com/wp-content/uploads/1305021426712.png"><img src="http://www.webgelistirici.com/wp-content/uploads/1305021426712.png" alt="" title="Tablo" width="199" height="100" class="aligncenter size-full wp-image-448" /></a></div>
<p>Şimdi sınıfımızı yazıp <code><strong>"vt.sinif.php"</strong></code> adıyla kaydedelim. Bu dosyanın içeriği aşağıda görülmektedir.</p>
<p><span id="more-439"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * vt.sinif.php
 * 
 * Veritabanı işlemleri için basit bir sınıf
 *
 * @author Bülent YILMAZ
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> vt <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$sunucu</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Veritabanı sunucusunun adresini tutacak</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$kullanici_adi</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$sifre</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$veritabani_adi</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Kullanılacak veritabanının adını tutacak</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$vt_baglanti</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// mysql_connect() fonksiyonundan dönen değeri tutacak</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$vt_kaynak</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// mysql_query() fonksiyonundan dönen değeri tutacak</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$sonuc</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// SELECT sorgularından dönen nesne dizisini tutacak</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Kurucu yöntem. Burada ilklendirme işlemleri yapılır.
     *
     * @param string $sunucu
     * @param string $kullanici_adi
     * @param string $sifre
     * @param string $veritabani_adi
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sunucu</span><span style="color: #339933;">,</span> <span style="color: #000088;">$kullanici_adi</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sifre</span><span style="color: #339933;">,</span> <span style="color: #000088;">$veritabani_adi</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sunucu</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sunucu</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">kullanici_adi</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$kullanici_adi</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sifre</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sifre</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">veritabani_adi</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$veritabani_adi</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_baglanti</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_kaynak</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sonuc</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Veritabanı bağlantısı yapılır.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> baglan<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Veritabanı sunucusuna bağlan</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_baglanti</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sunucu</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">kullanici_adi</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sifre</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Veritabanı sunucusu ile bağlantı gerçekleşti ise istenen veritabanını seçili hale getir</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_resource</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_baglanti</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">veritabani_adi</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Sorgu işletilir.
     *
     * @param string $sorgu
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sorgu_islet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorgu</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_kaynak</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorgu</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_baglanti</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * SELECT sorguları için nesne dizisi döndürülür
     * 
     * @return array
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> kayitlari_getir<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_kaynak</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_kaynak</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sorgu_fo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_kaynak</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sonuc</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sorgu_fo</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Her kayıt bir nesne olarak dizide toplanır</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sonuc</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Nesne dizisini döndür</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     *  Yıkıcı yöntem. Burada sonlandırma işlemleri yapılır.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_resource</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_baglanti</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vt_baglanti</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Veritabanı bağlantısını sonlandır</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sonuc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Nesne dizisi oluşturulmuş ise yoket</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Veritabanı sınıfımızı kullanarak birkaç örnek yapalım.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'vt.sinif.php'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Veritabanı sınıfımızın tanımlı olduğu dosyayı dahil edelim</span>
&nbsp;
<span style="color: #000088;">$vt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> vt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'123'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Veritabanı nesnemizi tanımlayalım</span>
&nbsp;
<span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">baglan</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Veritabanı bağlantısını kuralım</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// SELECT sorgusu</span>
<span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sorgu_islet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; SELECT * FROM kisi &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">kayitlari_getir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$kayit</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$kayit</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ad</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/*
Çıktı:
&nbsp;
Ahmet
Mehmet
Hasan
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// UPDATE sorgusu</span>
<span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sorgu_islet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; UPDATE kisi SET ad = 'Ziya' WHERE id = 1 &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// DELETE sorgusu</span>
<span style="color: #000088;">$vt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sorgu_islet</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; DELETE FROM kisi WHERE id = 1 &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-7.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-6</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-6.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-6.html#comments</comments>
		<pubDate>Fri, 06 May 2011 13:10:14 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=403</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>Tekilleştirme Tasarım Deseni (Singleton Design Pattern)</h2>
<p>Tekilleştirme tasarım deseni, veritabanı bağlantısı gibi, çalışma anında bir sınıfın sadece tek bir örneğinin yeterli olduğu durumlarda kullanılır. Tek bir nesne <code>static</code> olarak çağrılarak bellekte tutulur ve her ihtiyaç duyulduğunda nesneyi yeniden oluşturmak yerine bellekte daha önce oluşturulmuş olan nesne kullanılır. Bu da bize performans ve bellek kazancı olarak geri döner. </p>
<p>Tekilleştirme tasarım deseninin özelliği gereği bellekte yalnızca bir kez oluşturulur ve ikinci bir oluşturma girişimine izin verilmez. Tekil nesne bellekte tutulduğu için <code>static</code> olarak tanımlanma zorunluluğu vardır.</p>
<p><span id="more-403"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$nesne</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Bellekte sınıfın tek örneğini tutacak olan sınıf özelliği</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Kurucu yöntemler normalde public olarak tanımlanır.</span>
	<span style="color: #666666; font-style: italic;">// Tekilleştirme tasarım deseninin özelliği gereği burada kurucu metod private olarak tanımlanıyor.</span>
	<span style="color: #666666; font-style: italic;">// Burada amaç, nesnenin &quot;new&quot; işleci ile doğrudan oluşturulmasını engellemektir.</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Static olarak tanımlıyoruz ki; nesne oluşmamış olsa bile bu yöntemi çağırabilelim.</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    	<span style="color: #009900;">&#123;</span>
           	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$nesne</span> instanceof <span style="color: #000000; font-weight: bold;">self</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Sınıfın daha önceden oluşturulmuş bir örneği (yani nesne) yoksa</span>
           	<span style="color: #009900;">&#123;</span>
                   	<span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Nesneyi oluşturalım ve sınıfımızın tek örneğini tutan özelliğimize atayalım</span>
           	<span style="color: #009900;">&#125;</span>
           	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$nesne</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">// Örnek bir yöntem tanımlayalım</span>
    	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    	<span style="color: #009900;">&#123;</span>
    	    	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Merhaba'</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">// &quot;__clone&quot; yöntemini kullanarak dışarıdan bu sınıfın çoğaltılmasını engelliyoruz.</span>
    	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __clone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    	<span style="color: #009900;">&#123;</span>
          	<span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Nesne çoğaltılamaz!'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">E_USER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Kurucu yöntem <code>private</code> olarak tanımlı olduğu için nesne doğrudan oluşturulamaz.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ölümcül hata (fatal error) oluşur</span></pre></div></div>

<p>Nesneyi oluşturup örnek bir yöntemini çalıştırmak istersek;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> sinif<span style="color: #339933;">::</span><span style="color: #004000;">singleton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: Merhaba</span>
<span style="color: #666666; font-style: italic;">// veya</span>
sinif<span style="color: #339933;">::</span><span style="color: #004000;">singleton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: Merhaba</span></pre></div></div>

<p>Nesne çoğaltılamaz.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$nesne2</span> <span style="color: #339933;">=</span> clone<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nesne</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ölümcül hata (fatal error) oluşur</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-6.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-5</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-5.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-5.html#comments</comments>
		<pubDate>Wed, 04 May 2011 12:14:46 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=399</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>Üreteç Tasarım Deseni (Factory Design Pattern)</h2>
<p>Üreteç yöntemi, nesnelerin çalışma anında üretilmesini sağlar. Parametre olarak sınıf ismini alır, tanımlı olduğu dosyayı otomatik olarak sayfaya dahil eder ve nesneyi oluşturup geriye döndürür.</p>
<p>Tüm nesneleri, ayrı ayrı üretmek yerine tek bir sınıfın <code>static</code> bir yöntemi üzerinden üretmek içi bu tasarım deseni kullanılabilir. Böylece herbir sınıfı sayfaya yüklemek için, tanımlı oldukları dosyaları <code>include</code> ile tek tek sayfaya yazmaktan kurtuluruz. Ayrıca sınıflarımızın yolu değiştiğinde, <code>include</code> ile sınıf dosyasını yüklediğimiz her yerde yolu düzeltmek yerine, üreteç yöntemi içinde yolu düzelterek kolayca bu işin üstesinden gelebiliriz.</p>
<p><span id="more-399"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> nesne_uret <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// Tüm sınıflarımızın içinde tanımlı olduğu dizinin yolunu tutacak sabit</span>
   <span style="color: #000000; font-weight: bold;">const</span> DIZIN_YOLU <span style="color: #339933;">=</span> <span style="color: #0000ff;">'./siniflar/'</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> factory<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sinif_adi</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/* static olmak zorunda */</span>
   <span style="color: #009900;">&#123;</span>
       try
       <span style="color: #009900;">&#123;</span>
           <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">DIZIN_YOLU</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sinif_adi</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
           <span style="color: #009900;">&#123;</span>
               <span style="color: #666666; font-style: italic;">// Sınıfın tanımlı olduğu dosyayı dahil edelim </span>
               <span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">DIZIN_YOLU</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sinif_adi</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
               <span style="color: #666666; font-style: italic;">// Nesneyi üretip geriye döndürelim </span>
               <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$sinif_adi</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span>
           <span style="color: #b1b100;">else</span>
           <span style="color: #009900;">&#123;</span>
               <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Sınıf Bulunamadı!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span>            
       <span style="color: #009900;">&#125;</span>
       catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$hata</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#123;</span>
           <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hata</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/pre&gt;'</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// &quot;sinif&quot; isimli sınıf, &quot;./siniflar/sinif.php&quot; dosyası içinde tanımlı</span>
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> nesne_uret<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sinif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: 5</span></pre></div></div>

<p><code>"<strong>sinif.php</strong>"</code> içeriği;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
           <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">x</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-5.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kohana 3.0 Notları: Basit Bir Örnek</title>
		<link>http://www.webgelistirici.com/kohana-3-0-notlari-basit-bir-ornek.html</link>
		<comments>http://www.webgelistirici.com/kohana-3-0-notlari-basit-bir-ornek.html#comments</comments>
		<pubDate>Tue, 03 May 2011 15:02:42 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[Kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=373</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>Tablo yapısı</h2>
<p><code>"kisi"</code> adlı tablonun içeriği aşağıdaki gibi olsun.<br />
<a href="http://www.webgelistirici.com/wp-content/uploads/tablo.png"><img src="http://www.webgelistirici.com/wp-content/uploads/tablo.png" alt="" title="Tablo" width="204" height="104" class="aligncenter size-full wp-image-376" /></a></p>
<h2>Controller dosyası</h2>
<p><strong>Konum:</strong> <code>".../application/classes/controller/deneme.php"</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SYSPATH'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No direct script access.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Controller_Deneme <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$model</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Bu nesne özelliği, model nesnemizi tutacak</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$view</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Bu nesne özelliği, view nesnemizi tutacak</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> before<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Model nesnemizi tanımlayalım</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model</span> <span style="color: #339933;">=</span> Model<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// View nesnemizi tanımlayalım</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span> <span style="color: #339933;">=</span> View<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'view1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> action_index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// View içine model nesnemizin kisi_ad() yönteminin döndürdüğü değeri gönderelim</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ad'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">kisi_ad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// View nesnemizin HTML çıktısını ekrana basalım</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-373"></span></p>
<h2>Model dosyası</h2>
<p><strong>Konum:</strong> <code>".../application/classes/model/model1.php"</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SYSPATH'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No direct script access.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Model_Model1 <span style="color: #000000; font-weight: bold;">extends</span> Model
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * ID'si verilen kaydın &quot;ad&quot; kolonundaki değeri döndürür.
     * 
     * @param     integer      Kişi ID'si
     * @return    string       Kişi adı
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> kisi_ad<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$sonuc</span> <span style="color: #339933;">=</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span>Database<span style="color: #339933;">::</span><span style="color: #004000;">SELECT</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot; SELECT ad FROM kisi WHERE id = :id &quot;</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ad'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// İlgili kaydın &quot;ad&quot; kolonundaki değer</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$sonuc</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sonucu controller'a geri döndürelim</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>View dosyası</h2>
<p><strong>Konum:</strong> <code>".../application/views/view1.php"</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SYSPATH'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No direct script access.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Ad: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ad</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Controller'dan gelen değeri ekrana basalım</span></pre></div></div>

<h2>Sonuç</h2>
<p><code>http://localhost/kohana/deneme?id=2</code> adresine istekte bulunuyoruz ve aşağıdaki sonucu alıyoruz.</p>
<p><a href="http://www.webgelistirici.com/wp-content/uploads/sonuc1.png"><img src="http://www.webgelistirici.com/wp-content/uploads/sonuc1.png" alt="" title="Sonuç" width="108" height="29" class="aligncenter size-full wp-image-378" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/kohana-3-0-notlari-basit-bir-ornek.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-4</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-4.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-4.html#comments</comments>
		<pubDate>Mon, 02 May 2011 11:10:56 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[callStatic]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[magic]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[__call]]></category>
		<category><![CDATA[__clone]]></category>
		<category><![CDATA[__construct]]></category>
		<category><![CDATA[__destruct]]></category>
		<category><![CDATA[__get]]></category>
		<category><![CDATA[__invoke]]></category>
		<category><![CDATA[__isset]]></category>
		<category><![CDATA[__set]]></category>
		<category><![CDATA[__sleep]]></category>
		<category><![CDATA[__toString]]></category>
		<category><![CDATA[__unset]]></category>
		<category><![CDATA[__wakeup]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=356</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>Sihirli Yöntemler (Magic Methods)</h2>
<h2><code><strong>__construct()</strong></code></h2>
<p>Kurucu yöntemdir. Nesne oluşturulurken otomatik olarak çalıştırılır. Bu yöntem içine, nesnenin oluşturulma anında yapılması istenen ilklendirme işlemleri yazılabilir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$isim</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ad</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isim</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ad</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Bülent'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #666666; font-style: italic;">// Nesne oluşturulurken $nesne-&gt;isim özelliğinin değeri &quot;Bülent&quot; yapılır</span></pre></div></div>

<p>Bir çocuk sınıftan nesne oluşturulurken, ata sınıfın kurucu yöntemi işletilmez. İşletilmek istenirse çocuk sınıfın kurucu yöntemi içinde <code>parent::__construct();</code> yazmak gerekir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif2 <span style="color: #000000; font-weight: bold;">extends</span> sinif1
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Önce sinif1 sınıfının kurucu yöntemi işletiliyor</span>
        <span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-356"></span></p>
<h2><code><strong>__destruct()</strong></code></h2>
<p>Yıkıcı yöntemdir. Betik sonlanırken veya nesne <code>unset()</code> ile yokedilirken devreye girer. Hafızayı boşaltmak, veritabanı bağlantısını koparmak vs. gibi sonlandırma işlemleri buraya yazılabilir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$isim</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isim</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Bir çocuk sınıftan nesne oluşturulurken, ata sınıfın yıkıcı yöntemi işletilmez. İşletilmek istenirse çocuk sınıfın yıkıcı yöntemi içinde <code>parent::__destruct();</code> yazmak gerekir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif2 <span style="color: #000000; font-weight: bold;">extends</span> sinif1
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Önce sinif1 sınıfının yıkıcı yöntemi işletiliyor</span>
        <span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><code><strong>__call()</strong></code></h2>
<p>Sınıfın olmayan bir yöntemi çağrıldığında devreye girer.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ad</span><span style="color: #339933;">,</span> <span style="color: #000088;">$parametreler_dizi</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// $ad : tanımsız yöntem adı</span>
    <span style="color: #666666; font-style: italic;">// $parametreler_dizi : yöntem çağrılırken kullanılan parametrelerden oluşan dizi</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$ad}</span> isminde yöntem yok!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// $nesne-&gt;yontem() biçiminde olmayan bir yöntem çağrılırsa devreye girer</span></pre></div></div>

<h2><code><strong>__callStatic()</strong></code></h2>
<p>Sınıfın olmayan <code>static</code> bir yöntemi çağrıldığında devreye girer.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __callStatic<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ad</span><span style="color: #339933;">,</span> <span style="color: #000088;">$parametreler_dizi</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// $ad : tanımsız yöntem adı</span>
    <span style="color: #666666; font-style: italic;">// $parametreler_dizi : yöntem çağrılırken kullanılan parametrelerden oluşan dizi</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$ad}</span> isminde yöntem yok!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// sinif::yontem() biçiminde olmayan bir static yöntem çağrılırsa devreye girer</span></pre></div></div>

<h2><code><strong>__get()</strong></code></h2>
<p>Sınıfın olmayan bir özelliği çağrıldığında devreye girer.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ad</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// $ad : tanımsız özellik adı</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$ad}</span> isminde özellik yok!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><code><strong>__set()</strong></code></h2>
<p>Sınıfın olmayan bir özelliğine değer atanmaya çalışıldığında devreye girer.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ad</span><span style="color: #339933;">,</span> <span style="color: #000088;">$deger</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// $ad : tanımsız özellik adı</span>
    <span style="color: #666666; font-style: italic;">// $deger : atanmaya çalışılan değer</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$ad}</span> isminde özellik yok!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><code><strong>__isset()</strong></code></h2>
<p>Sınıfın olmayan bir özelliğinin <code>isset()</code> veya <code>empty()</code> fonksiyonu ile sınanması durumunda devreye girer.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __isset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ad</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// $ad : tanımsız özellik adı</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$ad}</span> isminde özellik yok!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><code><strong>__unset()</strong></code></h2>
<p>Sınıfın olmayan bir özelliğinin <code>unset()</code> fonksiyonu ile yokedilmeye çalışılması durumunda devreye girer.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __unset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ad</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// $ad : tanımsız özellik adı</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$ad}</span> isminde özellik yok!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><code><strong>__toString()</strong></code></h2>
<p>Nesnenin <code>echo</code> ile yazdırılmaya çalışılması durumunda devreye girer.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Nesnenin içeriğini formatlı olarak ekranda gösterelim</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/pre&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$nesne</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// şeklinde yazdırılmaya çalışılırsa devreye girecektir</span></pre></div></div>

<h2><code><strong>__clone()</strong></code></h2>
<p>Nesnenin <code>clon</code> operatörü ile çoğaltılması durumunda devreye girer.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __clone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Başka nesneye o anki değeri ile değil de, 0 değeri ile aktarılmasını sağlar</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// $nesne1-&gt;x : 1 , $nesne1-&gt;y : 2</span>
<span style="color: #000088;">$nesne2</span> <span style="color: #339933;">=</span> clone <span style="color: #000088;">$nesne1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// $nesne2-&gt;x : 0 , $nesne2-&gt;y : 2</span></pre></div></div>

<h2><code><strong>__sleep()</strong></code></h2>
<p>Nesne <code>serialize</code> işlemine tabi tutulursa devreye girer. Bu yöntemin, nesnenin sadece <code>serialize</code> işlemine katılması istenen özelliklerini içeren bir dizi döndürmesi beklenir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$z</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __sleep<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #666666; font-style: italic;">// Bu durumda sadece &quot;x&quot; ve &quot;y&quot; özellikleri serialize işlemine katılır</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><code><strong>__wakeup()</strong></code></h2>
<p>Nesne <code>unserialize</code> işlemine tabi tutulursa devreye girer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-4.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-3</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-3.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-3.html#comments</comments>
		<pubDate>Fri, 22 Apr 2011 11:48:24 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[class_alias]]></category>
		<category><![CDATA[class_exists]]></category>
		<category><![CDATA[get_class]]></category>
		<category><![CDATA[get_class_methods]]></category>
		<category><![CDATA[get_class_vars]]></category>
		<category><![CDATA[get_object_vars]]></category>
		<category><![CDATA[get_parent_class]]></category>
		<category><![CDATA[instanceof]]></category>
		<category><![CDATA[is_subclass_of]]></category>
		<category><![CDATA[method_exists]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[property_exists]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=334</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>Sınıf ve nesne işleçleri</h2>
<h2><code><strong>instanceof</strong></code></h2>
<p>Bir nesnenin bir sınıfın örneği olup olmadığını bulur. Bir nesnenin kalıtımla bağlı olduğu tüm ata sınıfları için <code>TRUE</code> sonuç döndürür.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>TRUE/FALSE &lt;= $nesne instanceof sinif</code></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> instanceof stdClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: bool(false)</span>
&nbsp;
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> instanceof stdClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: bool(true)</span></pre></div></div>

<p><span id="more-334"></span></p>
<h2>Sınıf ve nesne fonksiyonları</h2>
<h2><code><strong>property_exists()</strong></code></h2>
<p>Sınıf veya nesnenin, belirtilen özelliğe sahip olup olmadığını saptar.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>TRUE/FALSE &lt;= property_exists('sinif', 'ozellik')<br />
TRUE/FALSE &lt;= property_exists($nesne, 'ozellik')</code></p>
<p><code>isset()</code> işlevinden farklı olarak, özellik değeri <code>NULL</code> olsa bile <code>TRUE</code> sonuç döndürür.</p>
<h2><code><strong>method_exists()</strong></code></h2>
<p>Sınıf veya nesnenin, belirtilen yönteme sahip olup olmadığını saptar.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>TRUE/FALSE &lt;= method_exists('sinif', 'yontem')<br />
TRUE/FALSE &lt;= method_exists($nesne, 'yontem')</code></p>
<h2><code><strong>class_exists()</strong></code></h2>
<p>Sınıfın tanımlı olup olmadığını saptar.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>TRUE/FALSE &lt;= class_exists('sinif'[, FALSE])</code></p>
<p>İkinci parametre <code>FALSE</code> verilirse, <code>__auto_load()</code> devre dışı bırakılıp sınama yapılır.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">class_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sinif'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2><code><strong>get_class()</strong></code></h2>
<p>Nesnenin türetildiği sınıfın adını döndürür, parametre nesne değilse <code>FALSE</code> döndürür.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>sinif_adi/FALSE &lt;= get_class($nesne)</code></p>
<h2><code><strong>get_parent_class()</strong></code></h2>
<p>Nesne veya sınıfın ebeveyn sınıfının ismini döndürür.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>sinif_adi/FALSE &lt;= get_parent_class($nesne)<br />
sinif_adi/FALSE &lt;= get_parent_class('sinif')</code></p>
<h2><code><strong>get_class_vars()</strong></code></h2>
<p>Sınıfın <code>public</code> olarak tanımlı özelliklerini dizi olarak döndürür.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>dizi &lt;= get_class_vars('sinif')</code></p>
<h2><code><strong>get_class_methods()</strong></code></h2>
<p>Sınıfın yöntemlerini dizi olarak döndürür.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>dizi &lt;= get_class_methods('sinif')</code></p>
<h2><code><strong>get_object_vars()</strong></code></h2>
<p>Nesnenin <code>static</code> olmayan tüm özelliklerini dizi olarak döndürür.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>dizi &lt;= get_object_vars($nesne)</code></p>
<h2><code><strong>class_alias()</strong></code></h2>
<p>Sınıfa takma ad tanımlamaya yarar.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>class_alias('sinif_eski_ad', 'sinif_yeni_ad');</code></p>
<h2><code><strong>is_subclass_of()</strong></code></h2>
<p>Belirtilen sınıfın belirtilen nesnenin ebeveynlerinden biri olup olmadığına bakar.<br />
<strong style="text-decoration:underline;  font-size:11px;">Kullanımı:</strong><br />
<code>TRUE/FALSE &lt;= is_subclass_of($nesne, 'sinif')</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-3.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-2</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-2.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-2.html#comments</comments>
		<pubDate>Thu, 21 Apr 2011 15:41:22 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[autoload]]></category>
		<category><![CDATA[catch]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[static]]></category>
		<category><![CDATA[stdclass]]></category>
		<category><![CDATA[try]]></category>
		<category><![CDATA[__class__]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=291</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<h2>&#8220;<code><strong>Static</strong></code>&#8221; tanımlı nesne üyeleri</h2>
<p><code>new</code> işleci ile nesne oluşturmaya gerek kalmadan, &#8220;<code>::</code>&#8221; işleci ile doğrudan sınıfın <code>static</code> tanımlı üyelerine erişilebilir. Nesne oluşturmadan <code>static</code> yöntem doğrudan çağrıldığında yeni bir nesne oluşturulmadığı için, sınıfın kurucu (<code>construct</code>) ve yıkıcı (<code>destruct</code>) metodları işletilmez.</p>
<p><span id="more-291"></span></p>
<p><strong><code><strong>Static</strong></code> tanımlı nesne üyelerinin kullanım şekilleri:</strong><br />
<code>sinif::$ozellik (her yerden)<br />
self::$ozellik (sınıf içinden)<br />
parent::$ozellik (türetilmiş sınıf içinden)</code></p>
<p><code>sinif::yontem() (her yerden)<br />
self::yontem() (sınıf içinden)<br />
parent::yontem() (türetilmiş sınıf içinden)</code></p>
<p><code>static</code> tanımlı yöntemler içinde iken <code>$this</code> sözde değişkeni kullanılamaz (nesne oluşmadığı için). <code>static</code> tanımlı özelliklere ve yöntemlere, &#8220;<code>-&gt;</code>&#8221; işleci ile erişilmeye çalışılırsa mantıksal hata (<code>warning</code>) oluşur.</p>
<p><strong><code><strong>Static</strong></code> tanımlı nesne üyelerini tanımlama şekilleri:</strong><br />
<code>[public/private/protected] static $ozellik;<br />
[public/private/protected] static function yontem() { ... }</code></p>
<p>Aşağıda <code>static</code> tanımlı özellik ve yöntem kullanımı ile ilgili bir örnek görülmektedir:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000088;">$ozellik</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$ozellik</span><span style="color: #339933;">++;</span> <span style="color: #666666; font-style: italic;">// static özelliklere &quot;$this-&gt;&quot; yerine &quot;self::&quot; ile erişilir</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Nesne oluşturmadan sınıfın static özellik ve yöntemlerini doğrudan kullanabiliriz</span>
sinif<span style="color: #339933;">::</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> sinif<span style="color: #339933;">::</span><span style="color: #000088;">$ozellik</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı : 6</span></pre></div></div>

<p><code>Static</code> tanımlı yöntemleri nesne oluşturarak da kullanabiliriz ancak <code>static</code> tanımlı özellikler de bu kullanım mümkün değildir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Nesne oluşturarak da sınıfın static yöntemlerini kullanabiliriz</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Static özellikler bu şekilde kullanılamaz</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ozellik</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// HATA</span></pre></div></div>

<p>Son olarak da <code>static</code> tanımlı bir özellik bellekte tutulduğu için yeni bir nesne tanımlandığında <code>static</code> tanımlı özelliğin bellekte tutulan son değeri ile nesne oluşturulur. <code>static</code> tanımlı değişken, belleğe bir kere yüklenir ve her defasında aynı bellek alanından çağrılır. İlk kullanıldığında belleğe atanır ve bir dahaki seferler oradan kullanılır. Her defasında nesne örneği oluşmayacağı için bellek tasarrufu sağlanır. <code>new</code> işleci ile nesne daha sonra oluşturulsa bile, ilgili <code>static</code> değişkenin bellekteki son değeri geçerli olur.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">sinif<span style="color: #339933;">::</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 5 değeri 1 arttırılır</span>
&nbsp;
<span style="color: #b1b100;">echo</span> sinif<span style="color: #339933;">::</span><span style="color: #000088;">$ozellik</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı : 6</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Nesne oluştur</span>
&nbsp;
<span style="color: #b1b100;">echo</span> sinif<span style="color: #339933;">::</span><span style="color: #000088;">$ozellik</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı : 6 (5 değil)</span></pre></div></div>

<p>Sonuç olarak, hep aynı değeri tutan özelliklerin ve hep aynı sonucu döndüren yöntemlerin <code>static</code> olarak tanımlanması daha iyidir.</p>
<h2>Otomatik sınıf yükleme (Autoload)</h2>
<p>Sayfada herhangi bir sınıf çağrıldığında, o sınıfın tanımlı olduğu dosyayı bizim yerimize otomatik olarak sayfaya dahil (<code>include</code>) etmeye yarar. Bunun için ön koşul, sınıf tanımlarının bulunduğu dosyaların hep aynı dizin içinde bulunmasıdır. </p>
<p>Örneğin &#8220;<code><strong>kutu</strong></code>&#8221; isimli sınıf, &#8220;<code>../sinif/<strong>kutu</strong>.sinif.php</code>&#8221; dosyası içinde tanımlı ise;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Bu fonksiyon sayfa içinde herhangi bir yerde tanımlanır</span>
<span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sinif_adi</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;../sinif/<span style="color: #006699; font-weight: bold;">{$sinif_adi}</span>.sinif.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Nesneyi tanımlayalım</span>
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> kutu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sınıf çağrıldığı anda, sınıfın tanımlı olduğu dosya otomatik dahil edilir</span></pre></div></div>

<p>Böylece sadece sayfada kullanılan sınıfların dosyaları sayfaya dahil ediliyor. Kullanılmayan sınıf dosyaları dahil edilmiyor. Eğer sayfada hiç sınıf kullanılmamışsa hiçbir sınıf dosyası da gereksiz yere sayfaya dahil edilmemiş oluyor.</p>
<h2><code><strong>__CLASS__</strong></code> sihirli sabiti</h2>
<p>İçinde bulunulan sınıfın adını verir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Sınıf içinde herhangi bir yerde iken;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: sinif_adi</span></pre></div></div>

<h2><code><strong>__METHOD__</strong></code> sihirli sabiti</h2>
<p>İçinde bulunulan yöntemin ve o yöntemin ait olduğu sınıfın adını birlikte verir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Sınıfın herhangi bir yöntemi içinde iken;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__METHOD__</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: sinif_adi::yontem_adi</span></pre></div></div>

<h2>Öntanımlı <code><strong>stdClass</strong></code> sınıfı</h2>
<p>PHP&#8217;de öntanımlı olarak bulunan boş bir sınıf şablonudur.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// stdClass Object () , boş sınıf</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// stdClass Object () , boş sınıf</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// stdClass Object ( [scalar] =&gt; a )</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'b'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// stdClass Object ( [a] =&gt; 1 [b] =&gt; 2 )</span></pre></div></div>

<h2>Diziyi nesneye dönüştürme</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Aşağıdaki gibi çok boyutlu bir dizimiz olsun</span>
&nbsp;
<span style="color: #000088;">$dizi</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'a'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Bülent'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'b'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 	<span style="color: #666666; font-style: italic;">/* Elemanları dizi olan dizi */</span>
		        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		            <span style="color: #0000ff;">'x'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span>
		            <span style="color: #0000ff;">'y'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span>
		        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		            <span style="color: #0000ff;">'q'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span>
		            <span style="color: #0000ff;">'w'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20</span>
		        <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dizi</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: &quot;Bülent&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dizi</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'b'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: 10</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dizi</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'b'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: 15</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Dizileri nesnelere dönüştürerek kullanalım</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'a'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Bülent'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'b'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 	<span style="color: #666666; font-style: italic;">/* Elemanları nesne olan dizi */</span>
		        <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		            <span style="color: #0000ff;">'x'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span>
		            <span style="color: #0000ff;">'y'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span>
		        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	        	<span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	        	    <span style="color: #0000ff;">'q'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span>
	        	    <span style="color: #0000ff;">'w'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20</span>
		        <span style="color: #009900;">&#41;</span>
    	        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">a</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: &quot;Bülent&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">y</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: 10</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">q</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: 15</span></pre></div></div>

<h2>Nesneyi diziye dönüştürme</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$var1</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$var2</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$var3</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
    static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$var4</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">const</span> SABIT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'merhaba'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$nesne</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/pre&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
Çıktı:
&nbsp;
Array
(
    [var1] =&gt; 1
    [ *var2] =&gt; 2
    [ sinifvar3] =&gt; 3
)
// Protected özellik başında &quot;*&quot; ile gösterilir
// Private özellik başında sınıf adı ile gösterilir
// Static özellik ve sabit, diziye aktarılmaz
// Sınıfın yöntemleri tip dönüşümü ile aktarılamaz
*/</span></pre></div></div>

<h2>Nesneyi döngüde kullanma</h2>
<p>Nesneleri tip dönüşümü yapmadan doğrudan <code>foreach</code> ile birlikte kullanabiliyoruz.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000088;">$z</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$w</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'merhaba'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$nesne</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$anahtar</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$deger</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$anahtar</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' = '</span><span style="color: #339933;">.</span><span style="color: #000088;">$deger</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
Çıktı:
&nbsp;
x = 5
y = 6
// static özellik esgeçilir
// private ve protected özellikler esgeçilir
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Burada ise nesneyi önce diziye aktarıp sonra döngüye sokuyoruz</span>
<span style="color: #666666; font-style: italic;">// Bu durumda static özellik dışındaki özellikler diziye aktarılacaktır</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$nesne</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$anahtar</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$deger</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$anahtar</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' = '</span><span style="color: #339933;">.</span><span style="color: #000088;">$deger</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
Çıktı:
&nbsp;
x = 5
y = 6
sinifq = 8
*w = 9
*/</span></pre></div></div>

<h2>NYP uyumlu karar kontrol mekanizması (<code><strong>try-catch</strong></code>)</h2>
<p>PHP 5 ile gelen, nesne yönelimli programlama (NYP) uyumlu karar kontrol mekanizması;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">is_int</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Örnek bir koşul</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Sayı değil!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Hata tetikliyoruz</span>
        <span style="color: #666666; font-style: italic;">// veya</span>
        <span style="color: #666666; font-style: italic;">// trigger_error('Sayı değil!', E_USER_ERROR); // Hata tetikliyoruz</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$hata</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hata</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Bir başka örnek;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> hata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$hata</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$mesaj</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;p&gt;Hatanın oluştuğu dosya adı = '</span>
               <span style="color: #339933;">.</span> <span style="color: #000088;">$hata</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFile</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$mesaj</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;p&gt;Hatanın oluştuğu satır no = '</span>
               <span style="color: #339933;">.</span> <span style="color: #000088;">$hata</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$mesaj</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;p&gt;Hata mesajı = '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hata</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$mesaj</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;p&gt;Hata kodu = '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hata</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$mesaj</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
try
<span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Hata oluştu!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$hata</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span>hata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$hata</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;de Nesne Yönelimli Programlama-1</title>
		<link>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-1.html</link>
		<comments>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-1.html#comments</comments>
		<pubDate>Wed, 20 Apr 2011 12:30:22 +0000</pubDate>
		<dc:creator>Bülent Yılmaz</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[chaining]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[constant]]></category>
		<category><![CDATA[extends]]></category>
		<category><![CDATA[final]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[private]]></category>
		<category><![CDATA[protected]]></category>
		<category><![CDATA[public]]></category>
		<category><![CDATA[zincirleme]]></category>

		<guid isPermaLink="false">http://www.webgelistirici.com/?p=284</guid>
		<description><![CDATA[Makale içeriğini görmek için lütfen makale başlığına tıklayınız.]]></description>
			<content:encoded><![CDATA[<p>Nesne yönelimli programlama (object oriented programming-OOP) mantığı; programcının nesne içerisinde ne yazıldığını bilmeksizin sadece fonksiyonların yaptığı işi bilerek kullanmasıdır ve böylece programcılar arasındaki eşgüdümü sağlamayı kolaylaştırır. Bu yazılım geliştirme yaklaşımında, bir nesne yönteminin nasıl çalıştığını değil, nasıl kullanıldığını bilmek yeterlidir.</p>
<h2>Sınıf örneği (nesne) oluşturma</h2>
<p>Bir sınıftan türetilen nesneye, o sınıfın <strong>örneği</strong> (<em>instance</em>) denir. Nesne oluşturma işine ise <strong>örnekleme</strong> denir. </p>
<p>Sınıf örneği oluşturmak (nesne oluşturmak) için &#8220;<code>new</code>&#8221; işlecinden faydalanılır. Bir nesne, sınıf adı ile veya sınıfın kurucu (constructor) yöntemi ile tanımlanabilir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// kurucu yöntem ile nesne tanımlandı</span>
<span style="color: #666666; font-style: italic;">// bu kullanımda sınıf tanımlanırken kurucu yönteme parametre de geçirilebilir</span>
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//veya</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// sınıf adı ile nesne tanımlandı</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Not: Türetilen nesnenin, türetildiği sınıfla aynı isimde olmasında sakınca yoktur.</span>
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> x<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-284"></span></p>
<h2>Sınıf tanımlama</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$ad</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ad_ata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Setter method</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ad</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ad_getir<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Getter method</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ad</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ad_ata</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Bülent'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ad_getir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Sınıf üyeleri</h2>
<p>Bir sınıfın 2 tipte üyesi bulunabilir:</p>
<ul>
<li><strong>Özellik</strong> (<em>Property</em>) : Sınıfın değişken ve sabitleri</li>
<li><strong>Yöntem</strong> (<em>Method</em>) : Sınıfın fonksiyonları</li>
</ul>
<p>Aşağıda 4 adet üyesi bulunan bir sınıf görülmektedir:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Sınıfın özellikleri (2 adet)</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sınıfın özelliği</span>
    <span style="color: #000000; font-weight: bold;">const</span> SABIT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sınıfın özelliği</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Sınıfın yöntemleri (2 adet)</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fonk1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Sınıfın yöntemi</span>
    <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fonk2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Sınıfın yöntemi</span>
    <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>    
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Sınıf üyelerinin erişim niteleyicileri (Access Modifiers)</h2>
<p>Sınıf üyelerine erişimi denetlemeye ve sınırlandırmaya yararlar. Bilgi güvenliği ve oluşabilecek hataların minimize edilmesi ana amaçtır.</p>
<ul>
<li><code><strong>Public</strong></code> (Varsayılan) : Her yerden erişilebilir (sınırlama yok). Niteleyici belirtilmemişse, varsayılan olarak <code>public</code> olarak kabul edilir.</li>
<li><code><strong>Protected</strong></code> : Sadece kendi sınıfının içinden ve türetilen bir sınıf içinden erişilebilir. Dışarıdan erişilemez. <code>Private</code>&#8216;den tek farkı; kalıtımla değeri korunur.</li>
<li><code><strong>Private</strong></code> : Sadece kendi sınıfının içinden erişilebilir. Türetilen sınıf içinden ve dışarıdan erişilemez. Kalıtımla değeri korunmaz.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$ozellik1</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Türetilen sınıfa değeri aktarılmaz</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$ozellik2</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Türetilen sınıfa değeri aktarılır</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> sinif2 <span style="color: #000000; font-weight: bold;">extends</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> goster<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;ozellik1 = <span style="color: #006699; font-weight: bold;">{$this-&gt;ozellik1}</span>&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;ozellik2 = <span style="color: #006699; font-weight: bold;">{$this-&gt;ozellik2}</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif2<span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">goster</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
Çıktı:
&nbsp;
ozellik1 =
ozellik2 = 5
*/</span></pre></div></div>

<p>Sınıfın bir yöntemi içinde, sınıfın özelliği olarak tanımlanmamış bir değişkeni doğrudan kullanırsak, o değişken sadece o yöntem içinde geçerli olan yerel bir değişken olmuş olur. Sınıfın diğer yöntemleri tarafından kullanılamaz. Örneğin aşağıdaki gibi bir kullanım hataya neden olacaktır.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Bu değişken sadece bu yöntem içinde kullanılabilir (sınıf özelliği değil, yerel değişken)</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// HATA, buradan bu değişkene erişilemez</span>
    <span style="color: #009900;">&#125;</span>    
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Sınıf içinde bir özelliği tanımlamadan doğrudan kullanmamız durumunda, <code>public</code> olarak tanımlanmış gibi kabul edilecektir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Sınıf özelliklerini normalde burada tanımlamamız gerekir</span>
    <span style="color: #666666; font-style: italic;">// public $ozellik; // Bu satırı yoruma aldık</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ozellik</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Üstte tanımlanmadan kullanıldı</span>
        <span style="color: #666666; font-style: italic;">// Bu durumda bu nesne özelliği, public olarak tanımlanmış gibi kabul edilir</span>
&nbsp;
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ozellik</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: 5</span></pre></div></div>

<h2>Sınıf içinde sabit tanımlama</h2>
<p>Sınıf içinde tanımlanan sabit, hem <code>static</code> hem de <code>public</code> özellik gösterir. Yani &#8220;<code>::</code>&#8221; işleci ile sınıf adı ile kullanılarak (nesne tanımlanmamış olsa bile) her yerden çağrılabilir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">const</span> SABIT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">SABIT</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sınıf içinde kullanımı</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: 5</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Sınıf dışında kullanımı (nesne oluşturmadan)</span>
<span style="color: #b1b100;">echo</span> sinif<span style="color: #339933;">::</span><span style="color: #004000;">SABIT</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Çıktı: 5</span></pre></div></div>

<p><strong>Kullanım şekilleri;</strong><br />
<code>sinif::SABIT // Herhangi bir yerde kullanırken (sınıf içinde veya dışında farketmez)<br />
self::SABIT // Sınıf içinde kullanırken<br />
parent::SABIT // Sadece türetilmiş bir sınıf içinde kullanırken</code></p>
<h2>Kalıtım</h2>
<p>PHP’de sadece tekli kalıtım desteklenir yani bir sınıf sadece tek bir sınıftan kalıtılabilir.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> cocuk_sinif <span style="color: #000000; font-weight: bold;">extends</span> ata_sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8220;<code>final</code>&#8221; anahtar kelimesi ile bir sınıf kısırlaştırılabilir. Bu durumda o sınıftan başka bir sınıf türetilemez. Türetilmeye çalışılırsa ölümcül hata (<code>fatal error</code>) oluşur.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">final <span style="color: #000000; font-weight: bold;">class</span> cocuk_sinif <span style="color: #000000; font-weight: bold;">extends</span> ata_sinif
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Bu durumda &#8220;<code>cocuk_sinif</code>&#8221; adlı sınıftan yeni bir sınıf türetilemeyecektir.</p>
<p>PHP&#8217;de nesne yöntemlerinin üzerine yazılmasına (<code>overriding</code>) izin verilir. Yani türetilen sınıf içinde, ata sınıfın yöntemlerinden birisinin adı ile yeni bir yöntem tanımlanabilir ve bu durumda ata sınıf içindeki yöntemin üzerine yazılmış olur.</p>
<p>Üzerine yazılmasını engellemek istediğimiz yöntemleri &#8220;<code>final</code>&#8221; anahtar kelimesi ile tanımlarsak, o sınıftan türetilen diğer sınıflarda bu yöntem ile aynı isimde yeni bir yöntem tanımlanamaz. Aksi halde ölümcül hata (<code>fatal error</code>) oluşur.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ata_sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> cocuk_sinif <span style="color: #000000; font-weight: bold;">extends</span> ata_sinif <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// HATA</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Nesneyi çoğaltma</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// (1) Doğrudan atama (referans atama ile eşdeğerdir)</span>
<span style="color: #666666; font-style: italic;">// Birbirlerinden eşzamanlı olarak etkilenirler</span>
<span style="color: #666666; font-style: italic;">// Tek fark birine NULL atanırsa diğeri etkilenmez</span>
<span style="color: #000088;">$nesne2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$nesne1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// (2) Referans atama</span>
<span style="color: #666666; font-style: italic;">// Birbirlerinden eşzamanlı olarak etkilenirler</span>
<span style="color: #666666; font-style: italic;">// Birine NULL atanırsa ikisi de NULL olur</span>
<span style="color: #000088;">$nesne_ref</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span> <span style="color: #000088;">$nesne1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// (3) Kopyasını çıkarma</span>
<span style="color: #666666; font-style: italic;">// Birbirlerinden bağımsızdırlar ve etkilenmezler</span>
<span style="color: #000088;">$nesne3</span> <span style="color: #339933;">=</span> clone <span style="color: #000088;">$nesne1</span><span style="color: #339933;">;</span></pre></div></div>

<p><code>unset()</code>, her koşulda sadece işlem yapılan nesneyi siler.<br />
<code>NULL</code> atamak, işlem yapılan nesneyi ve o nesnenin referans ile ilişkili olduğu diğer nesneleri de <code>NULL</code> yapar.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nesne1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Sadece $nesne1 nesnesi silinir</span>
&nbsp;
<span style="color: #000088;">$nesne1</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Bu durumda $nesne_ref nesnesi de NULL olur</span></pre></div></div>

<h2>Yöntem zincirleme</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sinif <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		…
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* Zincirleme için gerekli */</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		…
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* Zincirleme için gerekli */</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> yontem3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		…
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* Zincirleme için gerekli */</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$nesne</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sinif<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Zincirleyerek 3 yöntemi arka arkaya çalıştırıyoruz</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// veya</span>
&nbsp;
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ayrı ayrı çalıştırıyoruz</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$nesne</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">yontem3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webgelistirici.com/phpde-nesne-yonelimli-programlama-1.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

