Đang Thực Hiện

149225 Advanced PHP Static OOP Advice

I need some help/advice on how to create an instance of an object and keep it alive for all future calls during the page request. Basically, the singleton pattern.

I have two objects that should only be instantiated once per execution: the database object and my log object. I have setup static functions in each to get an instance. This static function checks to see if the object exists, if not, creates it and then returns the created object.

What i have noticed however, that this method doesnt seem to be reliable when i begin to call the class from multiple classes. When class x calls one of my static methods, only one object is created, but when class y calls my static method again, a new instance is created.

I cant have this happening and i need to know what to do to keep it from occuring. I have tried combinations of static and global variables. What is the best solution?

Here is the static method code in one of the classes:

static public function instance() {

static $ObjDB

if(!isset($objDB)) {

$objDB = new Database()

}

return $objDB

}

(semi colons removed)

I only need advice on how to implement this so you do not need to worry about writting any code for me or implenting a solution.

Kỹ năng: Bất kì công việc gì, PHP

Xem thêm: singleton pattern, best writting, multiple instance, global solution, advice, writting methods, object function method, create pattern, php static page, object method, php help function, advanced database, per alive, implement php code, php singleton multiple, global php, singleton multiple database, php multiple singleton, implement functions php, return calls, php oop global, php oop code, exists php, call page php, advanced php database

Về Bên Thuê:
( 27 nhận xét )

Mã Dự Án: #1895404