0

anyone know how to create php class root directory just like wordpress does? it doenst have to be like wordpress.
ive got some folders

*include:
 -header.php
 -footer.php
*images
*forms
 -index.php

the problem is when i use php header the image doesnt link up properly, is there anyway to solve it.
i read some article to use $_SERVER['DOCUMENT_ROOT']. and how do i apply it globally?

2
  • Can you clarify what it is you're asking for? Are you trying to replicate WordPress' class loading strategy? Commented Jan 8, 2011 at 14:51
  • @Rob yeas something like. what about to use $_SERVER['DOCUMENT_ROOT'] will it work globally? Commented Jan 8, 2011 at 15:00

4 Answers 4

1

You have to define an include_path. The include_path works like $PATH on Windows and UNIX. When you ask for a file (with require, or include), PHP will try to find it in the current directory. If it's not found, then will start trying with the directories defined in include_path.

include_path is a PHP.ini environment variable, so you can modify it in your main php.ini file, in your .htaccess using php_value, or runtime using set_inclue_path.

When you've added, for example, the class folder, then you can run require('class/foo.php'); in any of your project's files, and it'll find your global class folder, only if you don't have another class folder there.

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure it has much to do with what you are looking for, but just in case it helps: In most websites I do from scratch I include the necessary php files in different folders (depending on language, section, etc) with a simple code. From the URL, I get the language and the section and page variables:

$root = $_SERVER['DOCUMENT_ROOT'];
$include_string = ":/$root:/$root/$lang:/$root/$lang/$section:/$root/$lang/$section/$page";
ini_set("include_path", ".:../:$include_string");

My code is a bit more complex because there are some more variables and shared folders, but it's just the same thing with some loops and conditionals.

Comments

1

The least weird way i've managed to do it:

  1. Add your app root (often $_SERVER[DOCUMENT_ROOT]) to the include_path (easily done in php.ini, apache config, or at runtime with set_include_path)
  2. define an __autoload function to require "classes/{$classname}.class.php"

You may need to make the files' names lower case; i forget whether __autoload gets passed the class name as lower case, but i seem to remember issues with that.

Comments

0

$_SERVER['DOCUMENT_ROOT'] will work globally.

$_SERVER is one of PHP's superglobals.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.