tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
1
ZUGRIFFE
259
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Kalito Kalito ist offline Mitglied Brokat
    Registriert seit
    Aug 2010
    Ort
    Leipzig
    Beiträge
    380
    hallo,

    habe eine Fragen, bzgl. der __autoload-Funktion.

    Wie muss ich die Funktion schreiben, das ich nicht jedes mal die Klasse bzw. den Pfad zur Klasse händig in der Funktion bekannt machen muss, sondern, wenn ich die Klasse erstellt habe, dies auch sofort nutzen kann.

    Danke und Gruß
     
    Ich bin keine Signatur! - Auch wenn`s so aussieht :) - Wirklich!

    Über ein Danke freut sich jeder

  2. #2
    Avatar von Raisch
    Raisch Raisch ist offline extraordinary bit
    Registriert seit
    Aug 2011
    Ort
    Berlin
    Beiträge
    333
    Zitat Zitat von Kalito Beitrag anzeigen
    hallo,

    habe eine Fragen, bzgl. der __autoload-Funktion.

    Wie muss ich die Funktion schreiben, das ich nicht jedes mal die Klasse bzw. den Pfad zur Klasse händig in der Funktion bekannt machen muss, sondern, wenn ich die Klasse erstellt habe, dies auch sofort nutzen kann.

    Danke und Gruß
    Zum Bleistift, kann man das so machen:
    Code PHP:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    function __autoload( $class )
    {
        if ( !class_exists( $class ) && !interface_exists( $class ) )
        {
            $paths = array(
                '/Pfad/zu/den/Klassen/class.'.$class.'.php',
                '/Pfad/zu/den/Interfaces/interface.'.$class.'.php',
                '/Pfad/zu/den/abstrakten/Klassen/abstract.'.$class.'.php'
            );
     
            foreach ( $paths as $file )
            {
                if ( is_file( $file ) )
                {
                    require $file;
                    return true;
                }
            }
     
            die( "<h1>Error: Class '<em>$class</em>' not found!</h1>" );
        }
     
        return true;
    }

    Oder auch als Klasse in dieser Form:
    Code PHP:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    
    /**
     * Class to load unloaded classfiles.
     *
     * @abstract
     * @author Rainer Schulz <admin@raischblog.de>
     * @link [url]http://raischblog.de[/url]
     * @copyright 2011 - Rainer Schulz
     * @license CC BY-NC-SA 3.0 <http://creativecommons.org/licenses/by-nc-sa/3.0/>
     * @version 2.2 02/12/2011 14:13
     */
    abstract class AutoLoad
    {
        private static $arrPath = null;
        private static $arrExt  = null;
     
        public static function init( $strClassPath,
                                     $strInterfacePath,
                                     $strLibraryPath,
                                     $strModelPath,
                                     $strViewPath,
                                     $strControllerPath,
                                     $strRepositoryPath )
        {
            self::$arrPath = array (
                'class'      => $strClassPath,
                'interface'  => $strInterfacePath,
                'library'    => $strLibraryPath,
                'model'      => $strModelPath,
                'view'       => $strViewPath,
                'controller' => $strControllerPath,
                'repository' => $strRepositoryPath
            );
     
            self::$arrExt = array(
                'class'     => '.class.php',
                'interface' => '.interface.php',
                'library'   => '.library.php'
            );
     
            spl_autoload_register( 'AutoLoad::loadClass' );
            spl_autoload_register( 'AutoLoad::loadLibrary' );
            spl_autoload_register( 'AutoLoad::loadController' );
            spl_autoload_register( 'AutoLoad::loadModel' );
            spl_autoload_register( 'AutoLoad::loadRepository' );
            spl_autoload_register( 'AutoLoad::loadView' );
            spl_autoload_register( 'AutoLoad::loadInterface' );
        }
     
        public static function loadClass( $strClass )
        {
            return self::registerClass(
                self::$arrPath['class'].$strClass.self::$arrExt['class']
            );
        }
     
        public static function loadInterface( $strClass )
        {
            return self::registerClass(
                self::$arrPath['interface'].$strClass.self::$arrExt['interface']
            );
        }
     
        public static function loadLibrary( $strClass )
        {
            return self::registerClass(
                self::$arrPath['library'].$strClass.self::$arrExt['library']
            );
        }
     
        public static function loadModel( $strClass )
        {
            return self::registerClass(
                self::$arrPath['model'].$strClass.self::$arrExt['class']
            );
        }
     
        public static function loadView( $strClass )
        {
            return self::registerClass(
                self::$arrPath['view'].$strClass.self::$arrExt['class']
            );
        }
     
        public static function loadController( $strClass )
        {
            return self::registerClass(
                self::$arrPath['controller'].$strClass.self::$arrExt['class']
            );
        }
     
        public static function loadRepository( $strClass )
        {
            return self::registerClass(
                self::$arrPath['repository'].$strClass.self::$arrExt['class']
            );
        }
     
        private static function registerClass( $strFile )
        {
            if ( is_file( $strFile ) )
            {
                require $strFile;
                return true;
            }
     
            return false;
        }
    }

    Starten kannst Du die Klasse so:
    Code PHP:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    AutoLoad::init(
        PATH_TO_CLASSES,
        PATH_TO_INTERFACES,
        PATH_TO_LIBRARIES,
        PATH_TO_MODELS,
        PATH_TO_VIEWS,
        PATH_TO_CONTROLLERS,
        PATH_TO_REPOSITORIES
    );

    Gruß
     

Ähnliche Themen

  1. Namespaces in Kombination mit __autoload
    Von ComFreek im Forum PHP
    Antworten: 1
    Letzter Beitrag: 13.09.10, 12:40
  2. Verzeichnissstruktur PHP5 __autoload
    Von R00Ki3 im Forum PHP
    Antworten: 2
    Letzter Beitrag: 30.07.07, 22:29
  3. __autoload() Prob
    Von Headymaster im Forum PHP
    Antworten: 2
    Letzter Beitrag: 28.12.06, 17:51