Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP 5.3 Autoloading and Namespaces
Old 01-15-2010, 06:22 PM PHP 5.3 Autoloading and Namespaces
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
I'm flummoxed a bit. I recently upgraded from PHP 5.2 to 5.3 and my class files no longer autoload. I realize that 5.3 changed in that it now allows namespaces, but I haven't been able to find any resources on changes to autoloading. Is 5.3 backward compatible or are namespaces required? Or is there some PHP setting that I need to enable (I'm using XAMPP 1.7.2 as my localhost and haven't changed any of the default settings)? Ideally, I'd like to have my classes autoload in both 5.2 and 5.3.

Here's an example of the format I'm using for autoloading:

function __autoload($className) {
$className = str_replace('_', '/', $className);
require_once('includes/'.$className.'.php');
}
And the class:

class Example_Class_Two extends Example_Class_One {

function __construct(){
echo "I'm a function.";
}

}
I haven't made any changes to the code from when it worked in 5.2, but don't hesitate to point out the patently obvious because it may be that I'm missing something simple. Thanks!
__________________
Want new web resources every day? - Follow me on
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
 
Register now for full access!
Old 01-15-2010, 06:45 PM Re: PHP 5.3 Autoloading and Namespaces
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I just tested auto loading on my local server (Apache 2.2 and PHP 5.3.1) and it appears to be working normally. Namespaces are not required, though if you are using them you should be careful which namespace your autoload function is defined under. Is your autoload function being called at all? Here is the code I used for testing:
PHP Code:
echo 'PHP Version: ' phpversion() . '</br>';

function 
__autoload($classname)
{
    echo 
'Loading class: ' $classname '</br>';
    eval(
"class $classname {
        function __construct()
        {
            echo '
$classname';
        }
          }"
);
}

$foo = new Foo(); 
and the output:
PHP Version: 5.3.1
Loading class: Foo
Foo
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 01-15-2010 at 06:51 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-15-2010, 07:20 PM Re: PHP 5.3 Autoloading and Namespaces
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
It might be that it isn't being loaded at all. For some reason I'm not able to get any output from the file. Did anything change for require_once() for PHP 5.3?
__________________
Want new web resources every day? - Follow me on
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 01-15-2010, 07:25 PM Re: PHP 5.3 Autoloading and Namespaces
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
So here's what it turned out to be: I never use short tags (<? ?>) and always use the full declaration (<?php ?>). For whatever reason, my autoloader file had short tags, which PHP 5.3 deprecated. I switched to the full declaration and bingo, it works!

Man, I've been staring at that for about three weeks.
__________________
Want new web resources every day? - Follow me on
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by VirtuosiMedia; 01-15-2010 at 07:37 PM..
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Reply     « Reply to PHP 5.3 Autoloading and Namespaces
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13714 seconds with 12 queries