• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

怎么自动加载类文件

php 搞代码 4年前 (2022-01-23) 10次浏览 已收录 0个评论

如何自动加载类文件!
现在用的是autoload(代码如下:),还有别的方法能自动加载类文件吗?除了include(具体文件路径)

PHP code

<!---ecms Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->function __autoload($className){if(file_exists('model/'.$className.'.class.php')){include_once('model/'.$className.'.class.php');}if(file_exists('Controller/'.$className.'.class.php')){include_once('Controller/'.$className.'.class.php');}$pre<p>4本文¥来源gao!%daima.com搞$代*!码$网9</p><pre>搞代gaodaima码

fix=$GLOBALS[‘groupName’]==’main’?”:’../’;if(file_exists($prefix.’core/’.$className.’.class.php’)){include_once($prefix.’core/’.$className.’.class.php’);}if(file_exists($prefix.’core/Smarty-3.1.6/’.$className.’.class.php’)){include_once($prefix.’core/Smarty-3.1.6/’.$className.’.class.php’);}if(file_exists($prefix.’core/Smarty-3.1.6/sysplugins/’.$className.’.php’)){include_once($prefix.’core/Smarty-3.1.6/sysplugins/’.$className.’.php’);}}

——解决方案——————–
你是不是嫌这比较麻烦?
其实是因为你类文件放的地方比较多,而且你上面应该是用if else if或switch…case。语句。
如果你嫌麻烦的话可以用个数组来保存所有的路径比如。

PHP code

$arr = array('classPath1','classPath2','classPath3');foreach($arr as $v){  $new_path = $v.$className.'.php';  if(file_exists($new_path)){    include_once($new_path);    break;  }} 


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:怎么自动加载类文件
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址