1

I am redirecting all http request to index.php , router.php and others config file are being called by index.php , I am having a router which will look for handler if the file exist but i wonder how i access the the handler's variable in my controller.

Here is my index.php , will include all the config file like db and site setting

index.php

require_once ('include/config.inc.php');
require_once ('include/mysql.inc.php');
require_once ('include/shared_function.inc.php');
require_once ('include/router.inc.php');

Here is my router , will check for handlers if exist.

router.php

front_controller(){

    if(file_exists('handlers/login-handler.php')){
        include_once('handlers/login-handler.php');
    }

    include_once( 'login.php' );
}
front_controller(); 

Here is the file i handle request , normally i will keep an array of errors to display back in form

handlers/login-handler.php

//initialise variable to keep error
$errors=array();

if(request==post){
...
//validate post data
...
}

after the handlers being called , here is my controller which will display back the form or result.

login.php

//here comes the error , undefined variable
print_r($errors);

Please suggest me a good way to overcome this problem ...

Leon Armstrong
  • 1,285
  • 3
  • 16
  • 41
  • Try to declare $errors=array(); in router.php before if(file_exists...). Is there a function around the code? – Maarkoize Dec 04 '13 at 08:32
  • @MarcelBalzer Thanks for your reply , the whole code is inside a function call front_controller() – Leon Armstrong Dec 04 '13 at 08:37
  • I am looking for a friendly approach , because i going to use it as a back-bone for a large application – Leon Armstrong Dec 04 '13 at 08:38
  • Around the router.php content or around a content of the other files? – Maarkoize Dec 04 '13 at 08:38
  • Sorry i should have make my question clearer , I am redirecting all http request to index.php , router.php and others config file are being called by index.php – Leon Armstrong Dec 04 '13 at 08:42
  • What do you mean with call? Is index.php including the files? Then all variables of index.php which are declared before the includes are also accessable in the included files, EXCEPT in functions. If you want to use a variable from outside a function, you have to declare the variable at the top of the function as global or the function needs a parameter where you can give the function the variable. – Maarkoize Dec 04 '13 at 08:46
  • Please refer to my latest question , thanks , if i understand you correctly , i should define the $errors in index.php? – Leon Armstrong Dec 04 '13 at 08:48
  • 1
    You can try it, yes. As long as I don't know the structure of the function() I can't give you a specific answer. – Maarkoize Dec 04 '13 at 08:51

2 Answers2

2

How about creating an abstract Error class with public "add_error" and "get_errors" methods?. As long as the class is imported into your code you may access it freely. It could go like:

abstract class Error_handler
{
private static $ERRORS=array();
public static function add_error($error) {self::$ERRORS[]=$error;}
public static function &get_errors() {return self::$ERRORS;}
}

The errors would be contained inside the static $ERRORS array and would never collide with any other name in your code.

The Marlboro Man
  • 971
  • 7
  • 22
  • I think abstract work much better , because i am using classes autoload too... Thanks , any pros if i am using this? – Leon Armstrong Dec 04 '13 at 08:50
  • 1
    Pros... Well, for starters, it is relatively encapsulated and you can reuse and augment to your liking as long as you provide the same public interface. You can, for example, enable logging to files or databases seamlessly, notify yourself via mail or text message when something goes wrong and even store the erorrs as objects themselves (with description, line or file) with little effort. Cons?. Well, there's one single error array though you can always subclass or redefine. Though I would personally avoid global like the plague, you can think of this as some kind of "protected" global array. – The Marlboro Man Dec 04 '13 at 08:56
1

Use below code before print the variable.

 global $errors;
Kumar V
  • 8,810
  • 9
  • 39
  • 58
  • Do it have any disadvantage? I am using this in a large application , will it affect other variable like classes's variable that named $errors too? – Leon Armstrong Dec 04 '13 at 08:29
  • It will affect everywhere. I mean in other files also if you include other files in the same page. – Kumar V Dec 04 '13 at 08:30
  • 1
    See this answer for the disadvantages of using `global` - http://stackoverflow.com/a/12446305/1209020 – Ryan Dec 04 '13 at 08:36
  • 1
    using global is frowned upon and claimed to be not a good design practice. – DevZer0 Dec 04 '13 at 08:39