I was wondering if anyone could help me understand this particular aspect of OO PHP as it has caught me out a few times now.
I have specified a var at the top of my file like so
$route = explode('/', $_SERVER["REQUEST_URI"]);
// Shorthand
$r1=$route[0]; $r2=$route[1]; $r3=$route[2];
I am then tring to use $r1 etc inside a function written just below the above code.
function edit($id)
{
$_SESSION['path'] = $r1 . "/" . $r2 . "/" . $r3;
require 'app/controller/edit.php';
new Controller($id);
}
The $r1,$r2, $r3 vars cannot be seen for some reason inside this function.
Notice: Undefined variable: r1 in C:\wamp\www\options.ex\public_html\app\options.php on line 77
If I were to pass the $r vars to the function, I'd image there would be no problem, but since they are declared globally I was wondering why they were not visible without doing this as their scope presumably is global?
Thanks.
EDIT - full code.
<?php
require_once 'load.php';
// Clean and prepare query string
$route = explode('/', $_SERVER["REQUEST_URI"]);
// Trim outer exmpty parameters caused by leading/trailing slash
if($route[count($route)-1]=='') unset($route[count($route)-1]);
if($route[0]=='') unset($route[0]);
$route = array_values($route);
// If any parameters are undefined, set them to ''
if(!isset($route[0])) { $route[0]=''; $route[1]=''; $route[2]=''; }
elseif(!isset($route[1])) { $route[1]=''; $route[2]=''; }
elseif(!isset($route[2])) { $route[2]=''; }
// Shorthand
$r1=$route[0]; $r2=$route[1]; $r3=$route[2];
// Choose route, else default to dash
if($r1=='dashboard' && $r2=='' && $r3=='') dashboard();
elseif($r1=='staff' && $r2=='add' && $r3=='') add_staff();
elseif($r1=='staff' && $r2=='edit' && $r3!='') edit_staff($r3);
else header("location: http://local.options.ex/dashboard");
// Dashboard: Main entry point after login.
function dashboard()
{
require 'app/controller/dashboard/dashboard.php';
new Controller();
}
// Staff related interfaces ----------------------------------------------------
function add_staff()
{
require 'app/controller/staff/add_staff.php';
new Controller();
}
// ----------------------------------------
function edit_staff($staff_id)
{
$_SESSION['path'] = $r1 . "/" . $r2 . "/" . $r3;
require 'app/controller/staff/edit_staff.php';
new Controller($staff_id);
}
// ----------------------------------------
Just to clear up, the $r* variables are not used again beyond here, hence the convenient use of storing in session.