Supposing the following function and call:
function doSomething( &$someArray ) {
// Do something to $someArray
}
$names=array("John", "Paul", "George", "Ringo");
doSomething($names);
Is there any way to get the name of the referenced array (in this case “names”) from within the function? I know that I could refactor to add another argument to the function for the name of the array variable, but that is just inviting bugs. The use case is that for each array $someSillyName the function needs to access the database table someSillyName.
Thanks.
There is no way to do this in PHP.
Moreover, you should avoid to use variable names for application logic. Imagine, someone refactors your $name to something else.. You could amend your array like this:
This should also help you in other parts of your application. If you really want to go this way, I’d recommend to create a Class that encapsulates this array and provides stuff like
getDatabaseName();andgetData();