TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Autoload classes? (http://www.talkphp.com/advanced-php-programming/1154-autoload-classes.html)

Haris 09-17-2007 09:46 PM

Autoload classes?
 
How do you auto include all the classes from a particular folder?

Tanax 09-17-2007 09:51 PM

PHP Code:

<?php
function __autoload($class_name) {
    require_once 
$class_name '.php';
}

$obj  = new MyClass1();
$obj2 = new MyClass2();
?>

This script attempts to create an object from the files MyClass1.php and MyClass2.php

Found here: http://se2.php.net/autoload

EDIT: Opps, that was for swe... well w.e you get the point..

Haris 09-17-2007 09:57 PM

Will this magic function search through all the directories from where I add this function?

Wildhoney 09-17-2007 10:26 PM

No, the magic autoload function works like so:
  • You attempt to initiate a new class
  • If it is not found then PHP calls your defined __autoload() function
  • You then perform the action to include (SSI) the file containing your class automatically
  • If found, PHP continues on and initiates the instance correctly
  • If not found, PHP will pull you about it saying it cannot find the class

In daz's example, if your class is called members then the file name that contains your class must be called members.php.

Use glob if you want to include all your class files on-the-fly. But autoload is the preferred method as what's the point of loading in all your class files from the word go if you're not going to use them on that page?

PHP Code:

define('CLASS_DIR''./classes/');

foreach(
glob(CLASS_DIR '*.php') as $szFilename)
{
    include_once(
$szFilename);


This would include ALL the PHP files in the classes directory into your PHP script if you wanted to take that route.

Haris 09-17-2007 11:58 PM

Thanks mate.

Sam Granger 04-12-2008 03:23 PM

Wildhoney, I've always wondered this but never asked. What does the sz stand for in the variable names? I always see you using this?

Tanax 04-12-2008 03:56 PM

Quote:

Originally Posted by Sam Granger (Post 13407)
Wildhoney, I've always wondered this but never asked. What does the sz stand for in the variable names? I always see you using this?

It's just a markup that makes it easier for the reader of the code to see that it's a string.

INT - $iNumber
STRING - $szWord
ARRAY - $aArray

There's are some more.. like objects and booleans, etc..
I think there was a thread about this, but anyways.. there's the answer to your question anyhow :-)

Garrett 04-12-2008 10:03 PM

I made a function for this a while back for a project I was doing. You define each of your classes, and you can select them through the function.
PHP Code:

include_select(array("upload""lastfm")); 

PHP Code:

function include_select ($array) {

    
$label = array("lastfm"  => array("file" => "lastfm.php"),
                    
"connect" => array("file" => "connect.php"),
                    
"upload" => array("file" => "uploader.php"),
                    
"imdb" => array("file" => "imdb.php"),
                    
"imdb_parser" => array("file" => "imdb_parser.php"),
                    
"pagination" => array("file" => "pagination.php"),
                    
"pagination-layout" => array("file" => "pagination-layout.php"),
                    
"page-layout" => array("file" => "page-layout.php"),
                    
"twitter" => array("file" => "twitter.php"));

    while (list(
$key) = each($label)) {
        if(
in_array($key$array)) {
            include(
"classes/".$label[$key]['file']);
        }
    }
    




All times are GMT. The time now is 08:55 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0