PHP Constant Arrays: Is It Possible?

  • Context: PHP 
  • Thread starter Thread starter John Creighto
  • Start date Start date
  • Tags Tags
    Arrays Constant Php
Click For Summary
SUMMARY

Creating constant arrays in PHP can be effectively achieved using static variables within functions. The discussion highlights two specific implementations: the connect_db() function, which initializes a database connection only once, and the getLanguageList() function, which retrieves a list of languages from a database table while maintaining the state of the array across multiple calls. This approach avoids the need for global variables and enhances code maintainability by encapsulating the array within the function scope.

PREREQUISITES
  • Understanding of PHP function scopes
  • Familiarity with static variables in PHP
  • Basic knowledge of database connections in PHP
  • Experience with executing SQL queries in PHP
NEXT STEPS
  • Explore PHP static variables in depth
  • Learn about PHP database connections using PDO
  • Investigate performance optimization techniques for PHP functions
  • Study best practices for managing global state in PHP applications
USEFUL FOR

PHP developers, software engineers, and anyone looking to optimize function performance and maintainability in PHP applications.

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.
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
6K
  • · 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