Efficient PHP auto-loading and naming strategies

前端 未结 3 699
感情败类
感情败类 2020-12-04 07:14

Like most web developers these days, I\'m thoroughly enjoying the benefits of solid MVC architecture for web apps and sites. When doing MVC with PHP, autoloading obviously

3条回答
  •  -上瘾入骨i
    2020-12-04 07:23

    I landed on this solution:

    I created a single script that traverses my class library folder (which contains subfolders for separate modules / systems), and parses the file contents looking for class definitions. If it finds a class definition in a php file (pretty simple regex pattern), it creates a symlink:

    class_name.php -> actual/source/file.php
    

    This lets me use a single, simple autoload function that needs only the class name and the path to the main symlink folder, and doesn't have to do any path/string manipulation.

    The best part is that I can rearrange my source code completely or add a new subsystem and just run the link generating script to have everything autoloaded.

提交回复
热议问题