Page 1 of 1
Trying To Create An Autoloader
Posted: Sat Jan 08, 2011 8:21 am
by Sjwdavies
Hi guys,
I'm trying to create an autoloader, so I can reference my PHP classes using namespaces that reflect a library folder structure.
I've used php.ini (on my host) to auto_prepend the file that contains the autoloader function, and my index has the folloing code in it:
Code: Select all
<?php
$object = new \lib\sql\functions();
echo ("Hellow World");
?>
However when I load the page, I get the errors:
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/findmyne/public_html/index.php on line 3
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/findmyne/public_html/index.php on line 3
Parse error: syntax error, unexpected T_STRING in /home/findmyne/public_html/index.php on line 3
Anyone any ideas where i'm going wrong?
Re: Trying To Create An Autoloader
Posted: Sat Jan 08, 2011 8:48 am
by Neilos
switch \ for /
Re: Trying To Create An Autoloader
Posted: Sat Jan 08, 2011 8:57 am
by Sjwdavies
Neilos wrote:switch \ for /
That's helped me progress somewhat...
Instead, now it just says:
Code: Select all
Fatal error: Class 'lib' not found in /home/findmyne/public_html/index.php on line 12
The classname it's looking for is lib.class.php
But I've just found out my host is running 5.2.13 - can I still achieve what i'm trying to?
Re: Trying To Create An Autoloader
Posted: Sat Jan 08, 2011 9:44 am
by Neilos
What is functions()? Is it a file called functions.php? Or is it a class that you define somewhere?
If you have a file called functions.php that resides at public_html/lib/sql/functions.php that contains a class called functions() that defines a list of functions try;
Code: Select all
<?php
include("lib/sql/functions.php");
$object = new functions();
echo ("Hello World");
?>
Re: Trying To Create An Autoloader
Posted: Sat Jan 08, 2011 10:02 am
by Sjwdavies
Neilos wrote:What is functions()? Is it a file called functions.php? Or is it a class that you define somewhere?
If you have a file called functions.php that resides at public_html/lib/sql/functions.php that contains a class called functions() that defines a list of functions try;
Code: Select all
<?php
include("lib/sql/functions.php");
$object = new functions();
echo ("Hello World");
?>
Hi - thanks for your help.
It's both a file and a class called functions.php. I don't want to use an include, that's the whole point of using the autoloader.
Re: Trying To Create An Autoloader
Posted: Sat Jan 08, 2011 10:59 am
by s992
I don't believe PHP support namespaces prior to 5.3.
Re: Trying To Create An Autoloader
Posted: Sat Jan 08, 2011 12:09 pm
by Sjwdavies
s992 wrote:I don't believe PHP support namespaces prior to 5.3.
I thought, and have read as much.
I suspect I've got it working, but as it doesn't support namespaces it's dropping the relevance after the slashes. Hence why it's looking for lib.class.php
Re: Trying To Create An Autoloader
Posted: Sat Jan 08, 2011 1:36 pm
by Neilos
s992 wrote:I don't believe PHP support namespaces prior to 5.3.
I wasn't aware of this.
Sjwdavies wrote:Hence why it's looking for lib.class.php
Yeah I noticed this, that's why I suggested the include.
There are examples of autoloaders here;
http://php.net/manual/en/language.oop5.autoload.php
Lots of these work with pre 5.3.0, I'm sure however you've seen them. If the problem is with the file hierarchy then surely one include is acceptable?

lol