Trying To Create An Autoloader

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sjwdavies
Forum Commoner
Posts: 25
Joined: Mon Nov 17, 2008 10:18 am

Trying To Create An Autoloader

Post 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?
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Trying To Create An Autoloader

Post by Neilos »

switch \ for /
Sjwdavies
Forum Commoner
Posts: 25
Joined: Mon Nov 17, 2008 10:18 am

Re: Trying To Create An Autoloader

Post 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?
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Trying To Create An Autoloader

Post 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");

?>
Sjwdavies
Forum Commoner
Posts: 25
Joined: Mon Nov 17, 2008 10:18 am

Re: Trying To Create An Autoloader

Post 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.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Trying To Create An Autoloader

Post by s992 »

I don't believe PHP support namespaces prior to 5.3.
Sjwdavies
Forum Commoner
Posts: 25
Joined: Mon Nov 17, 2008 10:18 am

Re: Trying To Create An Autoloader

Post 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
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Trying To Create An Autoloader

Post 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? 8O lol
Post Reply