Convert CamelCase to under_score_case in php __autoload()

后端 未结 2 1775
[愿得一人]
[愿得一人] 2021-01-30 22:30

PHP manual suggests to autoload classes like

function __autoload($class_name){
 require_once(\"some_dir/\".$class_name.\".php\");
}

and this a

2条回答
  •  野的像风
    2021-01-30 22:48

    You could convert the class name like this...

    function __autoload($class_name){
        $name = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $class_name));
        require_once("some_dir/".$name.".php");
    }
    

提交回复
热议问题