0
    <?php    
    public function run() {
    try {
        $db = $this->openDatabaseConnection();
        $this->dispatchToController($db);
    }
    catch(AccessDeniedException $e) {
        require_once 'app/controller/error.php';
        $controller = new ErrorController($db);
        $controller->accessDenied();
    }
    catch(NotFoundException $e) {
        require_once 'app/controller/error.php';
        $controller = new ErrorController($db);
        $controller->notFound();
    }
    catch(\Exception $e) {
        if(PRODUCTION) {
        require_once 'app/controller/error.php';
        $controller = new ErrorController($db);
        $controller->unknown();
        }
        
    }
}

the $db is defined on top, the snippet was run on ngnix web server with PHP 7.4.33 and gave the following error [500]: GET / - Uncaught ErrorException: Undefined variable: db

phperson
  • 21
  • 7
  • 4
    If `openDatabaseConnection` throws an exception, then `$db` is undefined. – shingo Aug 07 '23 at 14:53
  • im sure it can't be such a foolish mistake as I think I did defined $db lemme check again – phperson Aug 07 '23 at 14:55
  • ```'mysql:host=censoruser:censorpass@localhost:3306'``` all my credentials regarding to the database seems to be there, what could be the problem with this? (taken from ```openDatabaseConnection()```) – phperson Aug 07 '23 at 14:59
  • 1
    As shingo explained, this code can't work if `openDatabaseConnection` throws an exception: the `catch` blocks will use `$db` but the variable won't exist. I think you should use one separate `try … catch …` block for the database connection. – A.L Aug 07 '23 at 15:01
  • @A.L can you show an example for me, im following the guide you lastly directed me to (idk if that was you please correct me if not) but exceptions are too complicated for someone like me to understand. – phperson Aug 07 '23 at 15:09
  • 1
    Give `$db` a default value (e.g. `null`) before calling `$this->openDatabaseConnection()` – Barmar Aug 07 '23 at 15:13
  • 2
    Or define a new exception class that doesn't require a `$db` parameter, since this is for exceptions that prevent creating the DB connection in the first place. – Barmar Aug 07 '23 at 15:15
  • @Barnar thanks for the explanation, I get the error ```[500]: GET / - syntax error, unexpected '$controller' (T_VARIABLE)``` when ```$db``` is defined as ```null``` – phperson Aug 07 '23 at 15:50
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – Nico Haase Aug 07 '23 at 16:59
  • @NicoHaase I did have a look but my problem seems to be a bit different and cannot be answered with above. (please correct me if I didn't see it) – phperson Aug 07 '23 at 17:09

2 Answers2

0

You must to define $db out of try to use it in catch !

If you define $db out of function you need to global it in function with this :

global $db , $anyname;
cigien
  • 57,834
  • 11
  • 73
  • 112
AmiR
  • 18
  • 3
-3
public function run() {
    $db = $this->openDatabaseConnection();
    $this->dispatchToController($db);
    
    try {
    $db = null;
    }

    catch(AccessDeniedException $e) {
        require_once 'app/controller/error.php';
        $controller = new ErrorController($db);
        $controller->accessDenied();
    }
    catch(NotFoundException $e) {
        require_once 'app/controller/error.php';
        $controller = new ErrorController($db);
        $controller->notFound();
    }
    catch(\Exception $e) {
        if(PRODUCTION) {
        require_once 'app/controller/error.php';
        $controller = new ErrorController($db);
        $controller->unknown();
        }
        
    }
}

the solution was simple, in PHP you either have to make the variable global or use it outside of blocks for another block to access it, so I got $db outside of the try() block and that's a whole other story but I also had to rewrite the whole openDatabaseConnection() function.

phperson
  • 21
  • 7
  • There is already an accepted and correct answer. Adding another answer doesn't add any value. In addition your code is invalid, isn't it? You are missing the try clause. – hansfn Aug 11 '23 at 13:16
  • you can make it global and do it the same way, I tried to give a better understanding of the problem for some who'd occur the same issue, thanks for the negrepping. – phperson Aug 12 '23 at 20:22
  • It's better to comment or edit the existing answer making that better. In addition, your code is wrong. We are not negative - just aiming for quality answers without duplicates. – hansfn Aug 14 '23 at 06:04
  • @hansfn, thanks for the reply lol, I forgot to include the try block there tho as you said, I can sometimes be agressive tho sorry for the negativity of mine. – phperson Aug 14 '23 at 21:29