Home  >  Article  >  Backend Development  >  求助啊,快来帮帮忙呀。

求助啊,快来帮帮忙呀。

WBOY
WBOYOriginal
2016-06-23 14:26:51875browse

MVC  类继承 自动加载

最近在学习MVC开发模式。现在遇到个问题,就是自动加载类的问题。比如说我建一个控制器,AdminAction.php.然后我想让他继承同目录下的FirstAction.php.而且我想直接Class AdminAction extends FirstAction.不想先Include ‘FirstAction.php'.能不能用__autoload来实现啊。为甚ThinkPHP里面就是可以直接新建一个Action就直接写继承他的基类Action而不用引入啊。求大神帮忙开导下

回复讨论(解决方案)

<?php // PHP5 Used __autoload function $obj_A = new clsA(); // in "cls" directory! $obj_B = new clsB(); // in "cls/cls" directory! function __autoload($className){ if(strtolowwer($className) == "clsb"){ require_once "cls/cls/$className.php"; }else{ include_once "cls/$className.php"; } } ?> 

// ./myClass.php<?phpclass myClass {    public function __construct() {        echo "myClass init'ed successfuly!!!";    }}?>// ./index.php<?php// we've writen this code where we needfunction __autoload($classname) {    $filename = "./". $classname .".php";    include_once($filename);}// we've called a class ***$obj = new myClass();?>

一般好像是在使用多个类才这样使  

一般好像是在使用多个类才这样使  
哦哦。还是有点不解的样子。慢慢想想咯。哎呀老火,不过还是谢谢你啦

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn