php, mysql, html, css, js, c#, java, c++ coder

php oop(obje/nesne ye yönelik programlama) için 1-2 uygulama örneği

Mart 30, 2009 | In: Html, Php

obje ye yönelik kodlamanın biraz anlaşılması için bir örnek :
class.sayfalama.saho.php
PHP Kodu:

<?php
class sayfalama {

public

$sayfa;
public
$suan;
public
$geri;
public
$ileri;
public
$ileriDonguArtisi;
public
$ileriDonguLimit;
public
$bitis;
public
$baslangic;
public
$veriSayisi;
public
$sayfaVeriLimiti;
public
$geriLimit;
public
$ileriLimit;
public
$arguman;
public
$dosya;
public
$link;

public

$cssWidthDis = “675px”;
public
$cssWidthIc = “650px”;

public

$cssBg = “#fff”;
public
$cssColor = “#000″;
public
$cssFontSize = “11px/15px”;
public
$cssFontFamily = “tahoma”;
public
$cssBorderColor = “#eee”;

public

$cssHoverBg = “#4f9ecd”;
public
$cssHoverColor = “#fff”;
public
$cssHoverFontSize = “11px/15px”;
public
$cssHoverFontFamily = “tahoma”;
public
$cssHoverBorderColor = “11px/15px”;

function

cssWidthIc($string)
{
return
$this->cssWidthIc = $string;
}
function
cssWidthDis($string)
{
return
$this->cssWidthDis = $string;
}
function
cssBg($string)
{
return
$this->cssBg = $string;
}
function
cssColor($string)
{
return
$this->cssColor = $string;
}
function
cssFontSize($string)
{
return
$this->cssFontSize = $string;
}
function
cssFontFamily($string)
{
return
$this->cssFontFamily = $string;
}
function
cssBorderColor($string)
{
return
$this->cssBorderColor = $string;
}
function
cssHoverBg($string)
{
return
$this->cssHoverBg = $string;
}
function
cssHoverColor($string)
{
return
$this->cssHoverColor = $string;
}
function
cssHoverFontSize($string)
{
return
$this->cssHoverFontSize = $string;
}
function
cssHoverFontFamily($string)
{
return
$this->cssHoverFontFamily = $string;
}
function
cssHoverBorderColor($string)
{
return
$this->cssHoverBorderColor = $string;
}
function
css()
{
$html=‘<style type=”text/css”>
.sayfalama {
float:left;
width:’
. $this->cssWidthIc . ‘;
padding-top:5px;
padding-bottom:5px;
margin-left:5px;
display:inline;
}
.syfm {
float:left;
width:’
. $this->cssWidthDis . ‘;
display:inline;
padding:3px;
background:’
. $this->cssBg . ‘;
color:’
. $this->cssColor . ‘;
margin-top:5px;
font:bold ‘
. $this->cssFontSize . ‘ “‘ . $this->cssFontFamily . ‘”;
}
.syfm a #seciliSayfa
{
background:#cc0000;
}
.syfm a{
height:30px;
padding:3px;
background:’
. $this->cssBg . ‘;
color:’
. $this->cssColor . ‘;
margin-left:5px;
text-decoration:none;
border:1px solid ‘
. $this->cssBorderColor . ‘;
font:bold ‘
. $this->cssFontSize . ‘ “‘ . $this->cssFontFamily . ‘”;
}
.syfm a:hover{
padding:3px;
background:’
. $this->cssHoverBg . ‘;
color:’
. $this->cssHoverColor . ‘;
margin-left:5px;
text-decoration:none;
border:1px solid ‘
. $this->cssHoverBorderColor . ‘;
font:bold ‘
. $this->cssHoverFontSize . ‘ “‘ . $this->cssHoverFontFamily . ‘”;
}
</style>’
;
return
$html;
}
function
sayiDurum($sayi)
{
if(
substr($sayi,0,1)==‘-’)
{
return
false;
}
else
{
return
true;
}
}
function
geriLimit($sayi)
{
return
$this->geriLimit = $sayi;
}
function
ileriLimit($sayi)
{
return
$this->ileriLimit = $sayi;
}
function
veriSayisi($sayi)
{
return
$this->veriSayisi = intval($sayi);
}
function
sayfaVeriLimiti($sayi)
{
return
$this->sayfaVeriLimiti = intval($sayi);
}
function
suan($sayi)
{
return
$this->suan = (empty($sayi)) ? 0 : $sayi;
}
function
argumanlar($string)
{
return
$this->arguman = $string;
}
function
dosya($string)
{
return
$this->dosya = $string;
}
function
link()
{
return
$this->link = $this->dosya . “?” . $this->arguman;
}

function sayfa()
{
//try
//{
$this->sayfa = intval($this->veriSayisi / $this->sayfaVeriLimiti);
// if(!$this->sayfa)
// {
// throw new Exception(’sıfıra bölünemez’);
// }
//}
//catch (Exception $e) {
// echo ‘Hata: ‘, $e->getMessage(), “\n”;
//}
return $this->sayfa;
}
function
geri()
{
return
$this->geri = $this->suan - 1;
}
function
ileri()
{
return
$this->ileri = $this->suan + 1;
}
function
ileriDonguArtisi()
{
$this->ileriDonguArtisi = $this->geriLimit - $this->suan;
if(
$this->sayiDurum($this->ileriDonguArtisi))
{
return
$this->ileriDonguLimit = ($this->suan + $this->ileriLimit + $this->ileriDonguArtisi);
}
else
{
return
$this->ileriDonguLimit = ($this->suan + $this->ileriLimit);
}
}
function
bitis()
{
for(
$j=$this->suan;$j<$this->ileriDonguLimit;$j++)
{
if(
$j<$this->sayfa)
{
$temp = $j;
}
else
{
break;
}
}
return
$this->bitis = $temp;
}
function
baslangic()
{

for(

$k=$this->suan;$k>($this->suan-$this->geriLimit);$k–)
{
if(
$this->sayiDurum($k))
{
$temp = $k;
}
else
{
break;
}
}
return
$this->baslangic = $temp;
}
function
derle()
{
$this->sayfa();
$this->ileri();
$this->geri();
$this->ileriDonguArtisi();
$this->bitis();
$this->baslangic();
$this->link();

$html = null;
$html.= $this->css();
$html.=“<div class=’temizle’></div>”;
$html.=‘<div >
<div> Sayfalar:’
;
// buradaki linkleri – link title ve << , >> işaretlerinide class dan ayarlaniblir hale getirecez bir ara

if($this->sayiDurum($this->geri))
{
$html.=“<a href=’{$this->link}&sayfa=0′ title=’İlk son sayfaya git’>&laquo;&laquo;</a> “;
$html.=“<a href=’{$this->link}&sayfa={$this->geri}’ title=’Bir önceki sayfaya git’>&laquo; Geri</a> “;
}
//echo “basla : {$this->baslangic}<br>Bitiş {$this->bitis}<hr>”;

for($i=$this->baslangic;$i<=$this->bitis;$i++)
{
if(
$this->suan == $i)
{
$html.=“<a id=’seciliSayfa’>”.($i+1).“</a>”;
}
else
{
$html.=“<a href=’{$this->link}&sayfa={$i}’ title=’”.($i+1).“.Sayfaya git’>”.($i+1).“</a> “;
}
}
if(
$this->ileri<$this->sayfa)
{
$html.=“<a href=’{$this->link}&sayfa={$this->ileri}’ title=’Bir sonraki sayfaya git’>İleri &raquo;</a> “;
$html.=“<a href=’{$this->link}&sayfa={$this->sayfa}’ title=’En son sayfaya git’>&raquo;&raquo;</a> “;
}
$html.=“</div>
</div>”
;
return
$html;
}
}
?>

uygulaması :
PHP Kodu:

/// sayfalama yapılıyor
// $kriter burada id=’{$_GET['id']}’ veya baslik like ‘%{$_GET['kelime']}%’ gibi kriter belirtebilirsiniz.
$were = empty($kriter) ? “where {$kriter}”: null;
$saySql = mysql_query(“select count(id) from tablo $were”); // sayım yapılıyor
list($veriSayisi) = mysql_fetch_row($saySql);// değişkene atanıyor
include_once“class.sayfalama.php”;/// sdosya çağırılıyor
$sayfala = new sayfalama();/// sayfalama nesnesi tanımlanıyor
$sayfala->veriSayisi($veriSayisi);// max veri sayısı belirtiliyor
$sayfala->sayfaVeriLimiti(27);// 1 sayfada gözükecke veri limiti
$sayfala->ileriLimit(8);// örneğin 15. sayfada ilerilimit 8 yapılırsa aşağı barda 23 e kadar olan bir listeleme yapılır
$sayfala->geriLimit(6);// örneğin 15. sayfada gerilimit 6 yapılırsa aşağı barda 9 e kadar olan bir listeleme yapılır
$sayfala->suan($_GET['sayfa']);// şuan kaçıncı sayfada olduğu saptanıyor bununla
eğer şuanki sayfa ise link vermiyoruz tekrar dan farklı belirteçlerde kullanılabilir
$sayfala
->argumanlar(“bolum={$_GET['bolum']}&obje={$_GET['obje']}&kelime={$_GET['kelime']}”);// burada get le verilen argümanlar konuyor bunun yerine başka türlü nasıl yapacağınızı aşağıdaki notta bulabilirsiniz
$sayfala->dosya(“index.php”);// sayfalama linkinin verileceği sayfa
echo $sayfala->derle();// tanımlamaların hepsi derlenip ekrana yazdırılıyor
//// sayfalama bitiyor

not :
$sayfala->argumanlar(”bolum={$_GET['bolum']}&obje={$_GET['obje']}&kelime={$_GET['kelime']}”);
yerine
$argumanlar = array();
foreach($_GET as $key=>$val) $argumanlar[] = “{$key}={$val}”;
$argumans = implode(’&’,$argumanlar);
$sayfala->argumanlar($argumans);
gibi bir uygulama yapılabilir
css içerisine katmayadabiliriz ama renkleri vs
$sayfala->cssBorderColor(’#000000′);
gibi gibi yöntemlerle düzenleyebiliyorsunuz
ayrıca sayfalama.css gibi bir dosyayı sürekli sürekli yanında taşımamak için ben bu türlü kullanmaktayım
input u class olarak çağırmak için bir örnek :
class.html.saho.php
PHP Kodu:


class input
{
private
$name;
private
$id = false;
private
$value;
private
$type = “text”;
private
$text = false;
private
$class_ = “none”;

public function

__construct($name)
{
$this->name = $name;
}

function

name($string)
{
$this->name = $string;
}
function
id($string)
{
$this->id = $string;
}
function
type($string)
{
$this->type = $string;
}
function
value($string)
{
$this->value = $string;
}
function
text($string)
{
$this->text = $string;
}
function
class_($string)
{
$this->class_ = “class=’{$string}’”;
}
function
derle()
{
if(
$this->id == false)
{
$this->id = $this->name;
}

$html=“<input type=’{$this->type}’ name=’{$this->name}’ value=’{$this->value}’ id=’{$this->id}’ />”;
if(
$this->text != false)
{
$html.=“<label for=’{$this->id}’ {$this->class_}>{$this->text}</label>”;
}

return

$html;
}

kullanımı için :
[php]
include_once”class.html.saho.php”;
$input = new input(’kelime’);
$input->value($_GET['kelime']);
echo $input->derle();
gibi bir yöntemle
ekrana input yazdırabilirsiniz
diğer özellikler inide ->id,size,type belirtebilirsiniz ayrıca
$input->text(’kelime giriniz:’);
gibi bir text parametreside gönderirseniz yeni bir label de oluşturacaktır bu sayede
kelime giriniz: yazısına tıkladıklarında kelime input una otomatik focus lanacaklardır
bu örnekleri artırmak mümkün örneğin son input oluşturma örneğimiz objeye yönelik olmasaydı nasıl olurdu?
şöyle olur du :
PHP Kodu:

function input($name,$type,$id,$value,$ex=null) {
$html = “<input type=’{$type}’ name=’{$name}’ value=’{$value}’ id=’{$id}’ {$ex} />”;
return
$html;
}
echo
input(’saho’,‘type’,‘idcik’,‘valuecik’,‘…’);

gibi biraz daha karışık gibi duruyor name,type,id argüman sıralamasını ezbere bilmeniz gerekiyor kodlarınızı bir başkası kullanmak istediğinde öğrenmesi gerekebiliyor bu az satırlı bir kod kümesi bunun büyük bir proje olan halini düşündüğünüzde objye yönelik yazımın kod sade temizliğinin olduğuna anlışabilir olduğu , ekip olarak çalışıldığında anlaşılması ve kullanılması daha kolay olduğu gözükebiliyor
bir kaç örnek vermek istedim oop konusunda forumda oldukça fazla yazdık arama yaparak o konularada ulaşabilirsiniz.
not: eksikler olabilir örneğin sayfalama classı nı ilk önce function halinde yapmıştım baktım çok karışık oldu alel acele class a dökmüşüm eksiklerim olabilirm mükemmel değiliz en nihayetinde , alel acele paylaşma sebebimde msn den sürekli 1-2 arkadaş rahatsız edip oop hakkında birşeyler soruyorlar bu sebeple foruma ve blog yazayım herkez öğrensin dedim

Paylaş ve HalayÇek: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Technorati
  • Digg
  • del.icio.us
  • YahooMyWeb
  • Taggly
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • IndiaGram
  • Internetmedia
  • Netscape
  • Reddit
  • Scoopeo
  • StumbleUpon


2 Responses to php oop(obje/nesne ye yönelik programlama) için 1-2 uygulama örneği

Avatar

Uğur

Aralık 22nd, 2009 at 22:51

throw new Exception(’sıfıra bölünemez’);

catch (Exception $e) {
echo ‘Hata: ‘, $e->getMessage(), “\n”;
}
bu kodlar ne işe yarıyor tam olarak anlayamadım da

Avatar

admin

Aralık 23rd, 2009 at 00:38

sınıf / class lar ile çalışırken hatanın sebebini ve yerini bulmak genelde çok kolay olmuyor olası hata mesajı çıkacak yerlerde new exception ile hataları daha görsel daha okunabilir ve son kullanıcının anlayacağı şekilde hazırlayabiliriz bu amaçla burada kullanılıyor bu kısım bir kalıp olarak görebilirsin $e değişkenin 5-6 method u mevcut hata mesajı , hata satırını vs. alabilirsin hepsini görmek için print_r yada var_dump($e); ile hangi methodları içeriyor inceleyebilirsin.

Comment Form

Takvim

Temmuz 2010
Pts Sal Çar Per Cum Cts Paz
« Haz    
 1234
567891011
12131415161718
19202122232425
262728293031