Đ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 nhiều hơn: what is singleton pattern, what is a singleton pattern, the singleton pattern, how to create php, c# singleton pattern, best writting, what is object, what is an oop, php is object, oop oop, 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 create class, php singleton multiple, global php

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

ID dự án: #1895404