TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-17-2007, 09:46 PM   #1 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default Autoload classes?

How do you auto include all the classes from a particular folder?
Haris is offline  
Reply With Quote
Old 09-17-2007, 09:51 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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..
Tanax is offline  
Reply With Quote
Old 09-17-2007, 09:57 PM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Will this magic function search through all the directories from where I add this function?
Haris is offline  
Reply With Quote
Old 09-17-2007, 10:26 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-17-2007, 11:58 PM   #5 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Thanks mate.
Haris is offline  
Reply With Quote
Old 04-12-2008, 03:23 PM   #6 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

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?
Sam Granger is offline  
Reply With Quote
Old 04-12-2008, 03:56 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Sam Granger View Post
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
__________________
Tanax is offline  
Reply With Quote
Old 04-12-2008, 10:03 PM   #8 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 6
Thanks: 0
Garrett is on a distinguished road
Default

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']);
        }
    }
    

Garrett is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:58 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design