PHP PHP Constant Arrays: Is It Possible?

Click For Summary
Creating a constant array in PHP that doesn't need to be reinitialized each time a function is called can be achieved using static variables. This approach allows the variable to retain its value between function calls while remaining local to the function, enhancing code maintainability. In the provided examples, the `connect_db` function utilizes a static variable to store a database connection, ensuring it is only created once. The `getLanguageList` function similarly uses a static variable to cache the results of a database query, minimizing repeated database access. Both functions demonstrate how static variables can improve performance by avoiding unnecessary reinitialization while keeping the scope limited to the function itself.
John Creighto
Messages
487
Reaction score
2
I was wondering if was possible to create a constant array in php so it doesn't have to be reinitialized each time you enter a function. I guess one solution is to make it global but from a code maintenance perspective it makes since to put the array in with the function that uses it.
 
Technology news on Phys.org
If I understand your question correctly, you might be looking for a static variable.
I used it myself in the following two constructions (incidentally, from the same file, so I'll post them both):

PHP:
	function connect_db() {	
		static $theDatabase = null;
		if(!$theDatabase) {
			global $databaseProperties;
			include("database.php");
			$theDatabase = new db($databaseProperties['dbname'], $databaseProperties['host'], $databaseProperties['user'], $databaseProperties['password'], $databaseProperties['persist']);
		}
		return $theDatabase;
	}

	function getLanguageList() {
		static $languageList = null;
		global $db;
		if($languageList === null) {
			if(!$db) $db = connect_db();
			$languageList = $db->q("column SHOW COLUMNS FROM `TranslatedStrings`");
		}
		return $languageList;
	}

In both functions, the static variable only exists within the function, but not outside it. The value is kept between calls however. For example, I can call connect_db() as much as I like. The first time, it will create a new database handle, store it in the static variable and give it to me. However, every next time I call the function, it will see that the variable has already been assigned and will just return the value.

The second example is more similar to your question, that's why I included it. The first time, an array is initialized with some values from a database, a relatively slow operation. After that however, the function basically does nothing but "return $languageList;" where $languageList has the value from the first call.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
6
Views
3K