Tek Nesne Arayüz Sınıfı

index.php

PHP
17 lines
<?php
header('Content-Type: text/html; charset=utf-8');
require("Autoload.php");
class test implements Singleton_Interface{
public static function getInstance(){
return new static;
}
}
class test2{
public static function getInstance(){
return new static;
}
}
$b=new test;
if ($b instanceof Singleton_Interface) {
echo get_class(test::getInstance());
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Autoload.php

PHP
22 lines
<?php
if(!function_exists('__autoload')){
function __autoload($sinifAdi){
if(class_exists('Zend',false)){
if(is_int(strrpos($sinifAdi,'_Interface'))){
Zend::loadClass($sinifAdi);
}else{
Zend::loadInterface($sinifAdi);
}
}else{
if(!defined('__CLASS_PATH__')){
define('__CLASS_PATH__',realpath(dirname(__FILE__)));
}
$dosya=__CLASS_PATH__ . '/'.str_replace('_','/',$sinifAdi).'.php';
if(!file_exists($dosya)){
throw new Exception('Sınıf Dosyası Yüklenemiyor '.$sinifAdi);
}else{
require_once $dosya;
}
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Singleton/Interface.php

PHP
6 lines
<?php
if (!interface_exists('Singleton_Interface')) {
interface Singleton_Interface{
public static function getInstance();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Çıktısı

Markdown
1 lines
test
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 

Singleton Tek Nesne Genişleme Sınıfı

Bu sınıf, sınıfın sadece bir nesnesinin aynı anda var olmasını sağlar. Bir tekil sınıfının bir nesnesini oluşturmaya yönelik birden fazla girişim, her zaman aynı sınıf örneğini döndürür.

PHP
10 lines
class tekNesne{
public static function OrnekAl(){
return new static;
}
}
class os extends tekNesne{}
class man extends tekNesne{}
echo get_class(os::OrnekAl());
echo "<br>";
echo get_class(man::OrnekAl());
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Çıktısı

Markdown
2 lines
os
man
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX