- Impact
- 31
As many of you can probably see I haven't been around the forum in around a month or so. Been kind of busy upon other things and have been getting a lot of questions about doing different things in PHP. So this may be the first in possibly a series of tutorials to get all questions answered :p
In this tutorial I'll explain "define" pretty much and how to get it to define something like "ROOT_PATH". (not being so much a variable)
Unless you feel like defining everything in every file, I suggest the best way to define is make a file called something like "init.php". That's what I'll be using for the example.
init.php
Now make a directory call includes and then a file called function.php.
includes/function.php
Now finally just put this in an index.php or something so you can view it all done right
index.php
I've even attached a copy of the script for anyone interested.
Any questions, let me know
-RageD
In this tutorial I'll explain "define" pretty much and how to get it to define something like "ROOT_PATH". (not being so much a variable)
Unless you feel like defining everything in every file, I suggest the best way to define is make a file called something like "init.php". That's what I'll be using for the example.
init.php
PHP:
<?php
/**
* init.php for "Defining Paths in PHP" Tutorial by RageD
* (C) 2008 RageD
*
*/
/**
* This first one is simply for security. Accessing functions
* without proper access is bad :(
*
*/
define("SITE_SCRIPT", true);
/**
* Here we'll define the root path. Make sure that this file
* is in the ROOT directory! :)
*
*/
define("ROOT_PATH", dirname(__FILE__));
/**
* And finally.. Our includes. In this example it will be the
* "includes" directory.
*
*/
define("INC_PATH", ROOT_PATH . '/includes');
/**
* Final step... Make sure that this worked properly
* :)
*
*/
define("INIT_DONE", true);
?>
Now make a directory call includes and then a file called function.php.
includes/function.php
PHP:
<?php
/**
* includes/function.php for "Defining Paths in PHP" Tutorial by RageD
* (C) 2008 RageD
*
*/
// This is where that security definition comes into play :)
if(!defined("SITE_SCRIPT"))
{
print("Unauthorized Access!");
exit;
}
// Next, just a simple class and function example.
class Test
{
/**
* Constructor :)
*
*/
function __construct()
{
$this->text = "Hello World!<br />This script actually does work! :D";
}
/**
* Basically just print $this->text
*
*/
function hello()
{
echo($this->text);
exit;
}
}
?>
Now finally just put this in an index.php or something so you can view it all done right
index.php
PHP:
<?php
/**
* includes/function.php for "Defining Paths in PHP" Tutorial by RageD
* (C) 2008 RageD
*
*/
// Place this file in the ROOT directory with init.php
require_once("init.php");
// Check if it worked properly or not
if(!defined("INIT_DONE"))
{
echo("Doh! Houston, we have a problem. We did not initiate correctly!");
exit;
}
// Get our function :D
require_once(INC_PATH . "/function.php");
$test = new Test; // Define our class so we can perform the function.
$test->hello(); // Run the function!
?>
I've even attached a copy of the script for anyone interested.
Any questions, let me know
-RageD






