How php and own frameworks works in background? -


i new on web programming .i on c# .net platform on desktop. tried understand php , php frameworks confused little . understand php file can import classes in file php file require_once function. frameworks doesnt import own classes require_once function . think different dont understand.can explain me please?

most frameworks uses technique called "autoloading" automatically resolve , include needed dependencies.

an "autoloader" function called php when unknown class being referenced. "autoloader" can either procedurally create class or include external file based on file name.

the current (php 5.1.2 , higher) proper way of doing using spl_autoload_register(). here example of autoloader:

function autoload_example($classname) {   $normalizedname = strtolower($classname);    if(file_exists('includes/' . $normalizedname . '.inc')) {     require_once('includes/' . $normalizedname . '.inc');   } elseif(file_exists('includes/' . $normalizedname . '.inc')) {     require_once('includes/' . $normalizedname . '.php');   } else {     die('class ' . $classname . ' not found');   } }  spl_autoload_register('autoload_example');  $myawesomeobject = new awesome(); 

in example above, php run "autoloader" autoload_example when hit reference class awesome.

the "autoloader" first try file include/awesome.inc. if can find it, include it.

if not, file called include/awesome.php. if can find it, include it.

if not, die() stating not find awesome class.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -