PHP:MVC - Model View Controller | Application Class #2 & Loader #2

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$
Introduction:

We're continuing on coding our framework. In this part of the tutorial, I'll proceed with the loader and the blocking of the possible creation of new instances for the class App (or actually any class we might happen for someone or something to create an instance of). So let's witness that below.

The mechanism we want to make is to be able to obtain the name of the class along with its namespace, to locate where the class is located in the file system and to include it. So we need to find a way to 'connect' the namespace string with a psychical address of the server.

Code:
Please, Log in or Register to view codes content!
We need one variable that would actually contain an array and will store the registered namespaces. [This namespace, for that directory]:

Code:
Please, Log in or Register to view codes content!
The framework will have one namespace, controllers another, libraries another and so on. This will give us an enormous flexibility during the coding process of the future parts of our MVC framework. So now we need a method which will write to the array that is appended to the variable $namespace.

Code:
Please, Log in or Register to view codes content!
Basically, our method is the following. It accepts two parameters: the namespace itself and the psychical path. Then we begin the checks. First of all, of course, we trim the namespace because intervals in the beginning of them can cause immense trouble and we check the length of the namespace just to validate whether a valid namespace has been given/supplied.

Code:
Please, Log in or Register to view codes content!
If so, we throw an exception because obviously this is something we wouldn't want to happen. If there is a valid namespace we check for the path now. If there is a path supplied, we use realpath() function to get it. It both checks whether the path is correct and also returns its value which we could later on use. So then the variable $_path could be either true or false as a value. We check whether it's a directory and whether it is readable by PHP. If everything is alright, we append the value as a key to the array and for the path we include it itself.

Now we need to actually register the namespace so that we may use it accordingly.

Code:
Please, Log in or Register to view codes content!
So instead of hardcoding it in a configuration file, we use this little 'trick' with the dirname() function. In that case the framework could be located anywhere and registers by itself. Executing the code we have so far like so, we end up with the following error message:

Code:
Please, Log in or Register to view codes content!
The loader class is not located because we haven't yet written the method for it.

Let's see how this would look like.

Code:
Please, Log in or Register to view codes content!
It accepts one parameter which is actually the class that we want to load. When we have a situation when one method is calling another, it's better to make the implementation in that very method. When we have that sort of call-back function, (in our case autoload), we should call a new method and make the implementation there. So now we loop the namespaces to see whether any matches one of the so far registered. Let's put it simpler.

Code:
Please, Log in or Register to view codes content!
Let's say we have a registered namespace \KF\ and try to load a class that is located in that directory that I pointed out above. Then we need to check whether the namespace is in the beginning of the class that we're trying to load. It's also vital for it to be in the beginning in order to avoid collisions with the names. That's why we use the function strpos().

Now let's run it to see what we get and elaborate on the situation a bit more:

Code:
Please, Log in or Register to view codes content!
Since we need to specify the delimiter, that's obviously the backwards slash and we do it like so in the foreach() loop:
Code:
Please, Log in or Register to view codes content!
However, it would be much more wiser to append that to the method because every time where you register once but use many times, we should make the changes there, where we register not where we use. So this will result in the following:
Code:
Please, Log in or Register to view codes content!
We end up with the following output:
Code:
Please, Log in or Register to view codes content!
Conclusion:

I'm overall trying to keep all of the tutorials from these series in a certain length so as to be easily accessible my the members and to be read with bigger interest and expectation for the next part. Thanks for reading!
 
Top Bottom