PDA

View Full Version : مشتق گرقت از یک کلاس دیگه extends



saeed-71
سه شنبه 31 تیر 1393, 17:01 عصر
سلام.
ببینی این کلاس دیتابیسمه که میخوام ازش مشتق بگیرم برا کلاس سشنم.


public $connection;
public $host;
public $username;
public $password;
public $db;
public $dsn;
private $stmt;

public function __construct() {
if ($this->connection == NULL && !is_resource($this->connection)) {
$this->host = HOST;
$this->username = USER;
$this->password = PASS;
$this->db = NAME;
$this->dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->db . ';';
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8\''
);
try {
$this->connection = new PDO($this->dsn, $this->username, $this->password, $options);
} catch (PDOException $err) {
$this->connection = NULL;
echo '<pre>';
print_r($err);
echo '</pre>';
die;
}
}
}


حالا تو اون کلاس دیگم میخوام از کانکشن استفاده کنم.

اینجوری
کجاش اشتباه؟


<?php
class DBSessionHandler extends DB {
public $sessionTable = 'DBSession';
public $autoCreateSessionTable = true;
public $expire = 1200; // 20 minutes
public $autoStart = true;

/**
* Create the session table
*/
private function _createTable() {
$res = $connection->query("
CREATE TABLE `{$this->sessionTable}` (
`id` char(32) COLLATE utf8_bin NOT NULL,
`expire` int(11) DEFAULT NULL,
`data` longblob,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
");
$res->execute();
}


}

vahidqara
سه شنبه 31 تیر 1393, 17:42 عصر
سلام اینجارو ببینی بد نیست

http://barnamenevis.org/showthread.php?460951-%D9%85%D8%AC%D9%85%D9%88%D8%B9%D9%87-%DB%8C-%DA%A9%D9%84%D8%A7%D8%B3-%D9%87%D8%A7&highlight=%D9%85%D8%AC%D9%85%D9%88%D8%B9%D9%87+%DA %A9%D9%84%D8%A7%D8%B3+%D9%87%D8%A7

saeed-71
سه شنبه 31 تیر 1393, 19:07 عصر
تا اینجا رسوندمش.
کجاش اشتباهه؟


<?php
class DBSessionHandler {
public $sessionTable = 'DBSession';
public $autoCreateSessionTable = true;
public $expire = 1200; // 20 minutes
public $autoStart = true;

/**
* Create the session table
*/
/*private function _createTable() {
$res = $this->connection->query("
CREATE TABLE `{$this->sessionTable}` (
`id` char(32) COLLATE utf8_bin NOT NULL,
`expire` int(11) DEFAULT NULL,
`data` longblob,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
");
}

/**
* Deletes expired session
*/
private function _deleteExpired() {
$expire = time();
$res = $this->connection->prepare("DELETE FROM `sessiondb` WHERE (`expire`<`:expire)");
$res->execute(array(
":expire"=>$expire
));
}

/**
* Constructor
*/
public function __construct($autoStart = true) {
ini_set('session.save_handler', 'user');
$this->autoStart = $autoStart;
if($this->autoStart) {
$this->start();
}
register_shutdown_function(array($this, 'sessionClose'));
}

/**
* Update the current session ID with a newly generated one.
* @param boolean $deleteOldSession Whether to delete the old associated session values or not
*/
public function regenerateId($deleteOldSession = false) {
$oldId = session_id();
if(empty($oldId)) {
return;
}
session_regenerate_id();
$newId = session_id();
if(!$deleteOldSession) {
$res = $this->connection->prepare("UPDATE `sessiondb` SET `id`=:newId WHERE (`id`=:oldId)");
$res->execute(array(
":newId"=>$newId,
":oldId"=>$oldId
));
}
else {
$expire = time() + $this->expire;
$res = $this->connection->prepare("INSERT INTO `sessiondb` VALUES (:newId,:expire,'')");
$res->execute(array(
":newId"=>$newId,
":expire"=>$expire
));
}
}

/**
* Actually start the session
*/
public function start() {
@session_set_save_handler(
array($this, 'sessionOpen'),
array($this, 'sessionClose'),
array($this, 'sessionRead'),
array($this, 'sessionWrite'),
array($this, 'sessionDestroy'),
array($this, 'sessionGC')
);
@session_start();
if(session_id() == '') {
throw new Exception('Failed to start session.');
}
}

/**
* Ends the current session and store session data
* Do not call this method directly.
*/
public function sessionClose() {
$this->_deleteExpired();
if(session_id() !== '') {
@session_write_close();
}
}

/**
* Session destroy handler
* Do not call this method directly.
* param string $id session ID
* @return boolean whether session is destroyed successfully
*/
public function sessionDestroy($id) {
$res = $this->connection->prepare("DELETE FROM `sessiondb` WHERE (`id`=:id)");
$res->execute(array(
":id"=>$id
));
return ($res->rowCount() > 0);
}

/**
* Session GC (garbage collector) handler
* Do not call this method directly.
* @param integer $maxLifetime The number of seconds after which data will be seen as 'garbage' and cleaned up.
* @return boolean whether session is GCed successfully.
*/
public function sessionGC($id) {
$this->_deleteExpired();
return true;
}

/**
* Session open handler
* Do not call this method directly.
* @param string @savePath session save path
* @param @sessionName session name
* @return boolean Whether session is opened successfully
*/
public function sessionOpen($savePath, $sessionName) {
if($this->autoCreateSessionTable) {
$this->_deleteExpired();
if(DB::AffectedRows() < 0) {
$this->_createTable();
}
}
return true;
}

/**
* Session read handler
* Do not call this method directly.
* @param string $id session ID
* @return string the session data
*/
public function sessionRead($id) {
$expire = time();
$data = $this->connection->prepare("SELECT `data` FROM `sessiondb` WHERE (`id`=:id AND `expire`>=:expire)");
$data->execute(array(
":id"=>$id,
":expire"=>$expire
));
$data->fetchAll(PDO::FETCH_ASSOC);
return (count($data) > 0 ? base64_decode($data[0]['data']) : null);
}

/**
* Session write hanlder
* Do not call this method directly.
* @param string $id Session ID
* @param string $data session data
* @return boolean Whether session write is successful
*/
public function sessionWrite($id, $data) {
$data = base64_encode($data);
$expire = time() + $this->expire;
$res = $this->connection->prepare("INSERT INTO `sessiondb` VALUES (:id,:data,:expire) ON DUPLICATE KEY UPDATE `data`=:data,`expire`=:expire");
$res->execute(array(
":id"=>$id,
":data"=>$data,
":expire"=>$expire
));
}

/**
* Count online users
*/
public function onlineCount() {
$expire = time();
$data = $this->connection->prepare("SELECT COUNT(*) AS `sessiondb` FROM `sessiondb` WHERE (`expire`>=:expire)");
$data->execute(array(
":expire"=>$expire
));
$data->fetchAll(PDO::FETCH_ASSOC);
return $data[0]['total'];
}
public function onlineUsers() {
$expire = time();
$data = $this->connection->prepare("SELECT `data` FROM `sessiondb` WHERE (`expire`>=:expire)");
$data->execute(array(
":expire"=>$expire
));
$users = array();
foreach($data as $item) {
$item = base64_decode($item['data']);
if(preg_match('#username.*?"(.*?)"#i', $item, $match)) {
$users[] = $match[1];
}
}
return $users;
}
}