PHP:MVC - Model View Controller | Framework Configuration Routes #1

Prince

[ Verified Seller ]
Staff member
Trusted Seller
Joined
11 yrs. 6 mth. 27 days
Messages
5,381
Reaction score
18,380
Age
45
Wallet
11,590$
Now we know where the configuration folder is but the idea is that we could have more than one configuration files. So how would this class read all those files? There are three methods that I can personally come up with. One of them is whenever we make an instance of the class, the class to go through all configuration files, to obtain them and whenever we need them they are to be available somewhere in the memory. But nobody says that all the configurations files will be needed for the scope of a single request.

The second method is when we want to take something from a specific configuration file only then to include it and whenever we need it to take it from the memory. And the third is the worst scenario. It involves the inclusion of the configuration file/files we need every time. You can guess yourself why this is wrong.

Code:
Please, Log in or Register to view codes content!
So in two words, __GET will be called every time when we try to use a property of a class that is not defined. And as a parameter to __GET is gonna be the name of the property, which in our case is (app)

Code:
Please, Log in or Register to view codes content!
So whenever this method is executed, we'll know the name of the file that we want to load. Now with the calling of __get we first check this file hasn't already been loaded and we do this by checking whether the name of this file is a value of the elements from _configArray(); After this process is passes, we validate whether this name exists in the corresponding array and if the value exists we return the whole array.

Basically, _configArray() holds those two arrays:

Code:
Please, Log in or Register to view codes content!
Suppose we have the name we want to load in our $name variable that is a parameter for the __get method. So why not pass it as a variable here instead?

Code:
Please, Log in or Register to view codes content!
As a parameter to that method. Whenever we work with something that extracts the data, it's better not to have multiple dependencies but a single source. Now to our testing to check whether it works:

Code:
Please, Log in or Register to view codes content!
And modify the index.php to include it there:

Code:
Please, Log in or Register to view codes content!
Which will simply output the string 'Keeper'. So with a few LOC we managed to make a flexible mechanism for managing configuration files. Also another thing is that the answer is an array which means that we can manipulate it however we'd like to. Now, so as not to force the developer who is going to work with our framework to work only with the output variable in the app.php, we gonna return its value and I'm gonna talk more in-depth about this later. For now just return the value.

Code:
Please, Log in or Register to view codes content!
So whenever a folder is not located for the configuration files, we need to add such and apply it in our script. We can do that with just one conditional statement:

Code:
Please, Log in or Register to view codes content!
 
Top Bottom