logo
Blog single photo

Autoload classes in php


                        

                                            +-- index
|
+-- Control
      |
      +-- cont.php
           
      |
     dir1
      |-- cls1.php
      |
     dir2
      |-- dir3
           |-- cls2.php

dir1/cls1.php

 
 <?php  
 class cls1{ 
    function msg()
    { 
      echo "my message"; 
     } 
 } 
 ?> 

dir2/dir3/cls2.php
 <?php  
 class cls2{ 
      function msg2(){ 
           echo "thanks"; 
           } 
 } 
 ?> 

control/cont.php

 <?php  
 function __autoload($myclass){ 
      $dirlink = array('dir1/', 
                'dir2/dir3/'); 
           foreach ($dirlink as $calsses){ 
                if(file_exists($calsses.$myclass.'.php')) 
                { 
                     require_once ($calsses.$myclass.'.php'); 
                } 
           }  
           return; 
 } 
  ?> 

index.php

 <?php  
 include_once('control/cont.php'); 
 $mymsg = new cls1(); 
 $repmsg = new cls2(); 
 ?> 
 <p><?php $mymsg->msg(); ?> </p> 
 <p><?php $repmsg->msg2(); ?> </p> 

Output : my message

Top