Sitemde diğer sınıflara benim veritabanı sınıfını kullanma hakkında bir sorum var. Ben sadece benim veritabanı sınıfının bir nesne için ihtiyaç var ve bu benim bütün sorguları çalıştırmak istediğiniz örneğidir.
I have a class called MysqliExtension which extends and adds some additional functionality to the mysqli class
I also have a class called Users which manages my users log ons/offs, fetches their info from the database etc.
Finally, I have a file called config.php where I include these two classes and instantiate objects of the MysqliExtension and User classes.
config.php
require_once 'MysqliExtension.php';
require_once 'Users.php';
$database = new MysqliExtension(host,....);
Benim kullanıcı nesnesinin içine benim veritabanı nesne geçmek için en iyi yolu bazı görüşler almak istiyorum.
Option 1
Benim config.php dosyasında
$user = new User($username,password);
Benim Users.php dosyasında
class User{
function myfunction(){
global $database;
$database->callDatabaseFunction();
return $some_result;
}
}
}
Option 2
Benim config.php dosyasında
$user = new User($username,password,$database);
Benim Users.php dosyasında
class User{
function __construct($username,$password,$database){
$this->database = $database;
}
function myfunction(){
$this->database->callDatabaseFunction();
return $some_result;
}
}
}
Hangi yol en iyisi, ya da hemen hemen aynı? Tüm toghether Ben düşünce değil bu farklı bir yolu var mı?