1

I've been through the site regarding this subject but I'm still confused. I have fixed my problem but I know deep down I have kind of cheated and it's a fix that could cause me problems further down the line. Let me explain where I am:

Here is a directory structure folders like this >> [folder name] :

/site root
[ajax-loads]
   showorder.php

[bcls] << For bespoke classes
   class.order.php

[cls] << For classes
   class.main.php
   class.db.php
   class.sql.php
   ...
   ...

dashboard.php
index.php
config.php

...
.. etc etc

So above I'm showing I have 2 folders that contain classes for me to use of which most are called via an __autoload in class.main.php. The problem I am having is when I use jQuery to 'load()' new content in to a div; the behaviour changes and the path to the auto load is relative to the ajax-load file rather than relative to class.main.php. At the moment I have the following code where I feel I have cheated so to speak.

//PATHS & AUTOLOAD
set_include_path("./cls");
set_include_path("../cls");
set_include_path("./bcls");
set_include_path("../bcls");
//INVESTIGATE THIS NONESENSE ABOVE!

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

Earlier in the file I am having the same issue with my config.php file which I have temp fixed with:

set_include_path("./");
set_include_path("../");

Now let me point out this is my first big project having decided to get away from procedural coding and this is my first real experience with autoload. Rather than be a copy paste coder I really need to understand this better; I have hit a gap in my understanding here I feel. As well as the code being corrected, would someone kindly explain what the correct code is doing as I have tried a few 'solutions' but I am still getting odd results after mimicking other people's examples.

Love and kisses Andy

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
Andrew Myers
  • 451
  • 3
  • 13
  • I have tried to move things around so I call config.php and then everything happens from there. I thought it might be better to initialise things from a file that is in root but I still get the same problem; an ajax load will not look for the file relative to root it's relative to the ajax-load folder even though the require_once command is in the config.php file which is sat in root and works for all other situations – Andrew Myers Dec 19 '11 at 11:50

1 Answers1

0

If you really want to modify the include path then you should use it like:

define('BASE_PATH', $_SERVER['DOCUMENT_ROOT']);

// ...

set_include_path(
    BASE_PATH . '/bcls' .
    PATH_SEPARATOR . BASE_PATH . '/cls' .
    PATH_SEPARATOR . get_include_path() .
    PATH_SEPARATOR . '.'
);

Although, I would recommend that you rename your folders. It is strongly discouraged to abbreviate variables, classes, functions, folders, etc like that. Have you considered using a PHP framework of some sort? Doing so might help you in your endeavor, and you could avoid reinventing the wheel.

If you want light-weight, then consider using Kohana or CodeIgniter. Otherwise I recommend Zend Framework.

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
  • This is more of a learning thing, I have played with CodeIgniter before but I would like to get to a stage where I could build my own frame work (in theory) for a better understanding. – Andrew Myers Dec 19 '11 at 10:45
  • I don't really understand what you have posted up there and I'm still having a problem calling my config file in root with the message: Warning: require_once(config.php) [function.require-once]: failed to open stream: No such file or directory in G:\www\MiX\cls\class.main.php on line 14 Fatal error: require_once() [function.require]: Failed opening required 'config.php' (include_path='G:/www/G:/www//bcls;G:/www//cls;.;C:\php5\pear;.') in G:\www\MiX\cls\class.main.php on line 14 – Andrew Myers Dec 19 '11 at 10:48