• 27-06-2015, 03:08:54
    #1
    Merhabalar OOP'ye giriş için kendimi geliştiriyorum daha önce hiç kullanmadığım class yapısı bana sorun çıkarıyor genelde

    dbhelper classinin içindeki conn değişkeninin değerini null çekiyor , bu neden oluyordur ve bunu çözmem için yapman gerekenler nelerdir?

    dbhelper sınıfı :
    <?php 
    	class dbhelper
    	{
    		private $dbname = "anket";
    		private $dbuser = "root";
    		private $dbpassword = "";
    		private $dbhost = "localhost";	
    		protected $conn;
    		
    		
    		public function __construct() {
    			$ctrlConn = $this -> connect();
    			if($ctrlConn) {
    				if(mysqli_select_db($this->conn,$this->dbname)) {
    				} else die("Veritabanı Seçilemedi");
    			} else die("Veritabani'na bağlanılamadı.");
    		}
    		
    		private function connect() {
    			$this -> conn = @mysqli_connect($this -> dbhost,$this -> dbuser,$this -> dbpassword);
    			if($this -> conn) return true;
    			else return false;
    		}
    		
    		private function close() {
    			if($this -> conn) {
    				if(@mysqli_close($this->conn)) {
    				} else die("Veritabanı bağlantısı kapatılamadı.");
    			}
    		}
    		public function __destruct() {
    			$this -> close();
    		}
    		
    	}
    ?>
    Sql Worker sınıfım :
    <?php 
    	include_once('dbhelper.php');
    	class sqlWorker extends dbhelper
    	{
    		private $selectedTable;
    
    		
    		public function __construct($selTable=NULL) {
    			if($selTable) $this -> table($selTable);
    		}
    		public function table($table=NULL) {
    			if($table) $this -> selectedTable = $table;
    			else return $this -> selectedTable;
    		}
    		public function get($cols="*",$filter=NULL,$limit=NULL) {
    			if($limit) $limit = "LIMIT $limit";
    			if($filter) $filter = "WHERE $filter";
    			$sql = "SELECT $cols FROM ".$this -> selectedTable." $filter $limit";
    			$exSql = mysqli_query($this -> conn,$sql);
    			if($exSql) {
    				$readSql = mysqli_fetch_assoc($exSql);
    				return $readSql;
    			} else die("SQL sorgusunda bir hata oluştu");
    		}
    	}
    ?>
    Buna index sayffasının içeriği
    <?php 
    	header('Content-Type: text/html; charset=utf-8');
    	include("class/sqlWorker.php");
    	$test = new sqlWorker("survey");
    	$veriler = $test -> get();
    ?>
  • 27-06-2015, 13:03:36
    #2
    dbhelper abstract class oldugu icin construct methoduna erisim saglanmaz.


    class sqlWorker extends dbhelper {
    function __construct($selTable=NULL) {
    parent::__construct();
    if($selTable) $this -> table($selTable);
    }
    }


    olarak bir dene.

    bu arada, Is yapan classlarin abstract olarak kullanilmasi, mimari acidan kabul edilmiyor. abstract classlar genelde polymorphismi kuvvetlendirmek icin kullanilir.
  • 28-06-2015, 22:49:21
    #3
    Teşekkür ederim bahsettiğin gibi yapınca oldu , ayrıca sorunu tam olarak anladım sayende tekrar teşekkürler.