PHP:MVC - Model View Controller | Loader Class #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$
he next thing we have to do is to parse this through realpath() because it will check whether it exists and will return us a boolean result:

Code:
Please, Log in or Register to view codes content!
So basically we are checking whether the return value is true and if so and is readable by PHP, we can easily include the file. Now all this looks kinda of an untidy and messy code, so let's structure it a bit and shorten it:

Code:
Please, Log in or Register to view codes content!
Now if everything goes as it should, the loop is escaped. Also a good practice is when you obtain the desired result from a cycle, break the iteration. In case all namespaces have been processed and none correspond to the above conditions, is that PHP will throw an error that the file couldn't be located but we'll deal with that in the future when we're going to implement our error handling and reporting mechanisms. Let's see if that works actually.

Let's make a new class which will be called Test() and will be located in KF. Then try to load it in our index.php:

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

And the content of that class would be totally empty with just the namespace on the top:

Code:
Please, Log in or Register to view codes content!
We don't get an error because the class itself has no realization. The idea is that this class has been included by our mechanism and PHP did not throw any errors. Now let's make another class called User() with the following content:

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

Giving it a randomly typed namespace that does not exist and now let's try to call it in our index.php:

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