OK, so I need some more help.

In C# and other C languages, predefined methods throw excptions for you so all you have to do is catch them.

whereas in php, it seems, you have to throw your own.

try
		{
			if (!@include(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/db.class.php"))
				throw new Exception("Failed to load ".self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/db.class.php");
			require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/dbTables.class.php");
			require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/dbArrays.class.php");
			require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/utilities.class.php");
			require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/products.class.php");
			require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/encryption.class.php");
		}
		catch (Exception $e)
		{
			$error = "File: ".$e->getFile()." Line: ".$e->getLine();

		die( $error, $e->getMessage() );
		exit();
	}

OK, so this works. But when you include an invalid file with include but leave off the "@" you get an ERROR warning or whatever.

How do I capture this warning or error and put it into a variable, as using a "@" before a function eliminates it's E_ERROR level completely.

    This sounds interesting. From what I've found, you can trap such errors this way:

    try
    {
        $error = error_get_last();
        @include self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/db.class.php";
        $last_error = error_get_last();
        if ($last_error !== $error)
        {
            throw new Exception('Failed to load: ' . $last_error['message']);
            $error = $last_error; // In case you want to trap more errors.
        }
    
    require self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/dbTables.class.php";
    require self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/dbArrays.class.php";
    require self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/utilities.class.php";
    require self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/products.class.php";
    require self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/encryption.class.php";
    }
    catch (Exception $e)
    {
        $error_message = 'File: ' . $e->getFile() . "<br />\nLine: " . $e->getLine()
                       . "<br />\n" . $e->getMessage();
    
    die($error_message);
    }

    Note that [man]error_get_last/man is only available since PHP 5.2.0, so it may not work on some recent PHP installations. Also, you do not need to suppress errors with the @ operator if you set display_errors to Off in php.ini.

      Ah, yes... I undrstand that. Maybe I'm asking the wrong questions...

      In c# you can simply do this:

      
      try 
              { 
                  include(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/db.class.php"); 
                  require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/dbTables.class.php"); 
                  require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/dbArrays.class.php"); 
                  require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/utilities.class.php"); 
                  require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/products.class.php"); 
                  require(self::MAIN_ABS_PATH . self::SITE_DIR . self::CLASS_DIR . "/encryption.class.php"); 
              } 
              catch (Exception $e) 
              { 
                  $error = "File: ".$e->getFile()." Line: ".$e->getLine(); 
      
              echo $e->getMessage();
              exit(); 
          }
      
      

      ... C# creates the Exception for you, all you have to do is handle it!

      So I want one single exception to handle all usually "identical" errors in the try block.

      How would that be done?

        So I want one single exception to handle all usually "identical" errors in the try block.

        How would that be done?

        You will have to repeat my example for each include/require.

        Actually, there's a flaw: error_get_last() (and $php_errormsg, I believe) tracks the last even across scripts, so the check that the error was not identical may not be accurate.

        In short, you are looking at a shortcoming of PHP 5 due to its PHP 4 legacy (where exceptions did not exist).

          Ha ha, I have found another serious floor!

          
          try
          		{
          $clasDir = MAIN_ABS_PATH . SITE_DIR . CLASS_DIR;
          			if (!@include($clasDir . "/db.class.php"))
          			{
          				throw new Exception("Failed to load " . $clasDir . "/db.class.php");
          			}
          
          		unset($clasDir);
          	}
          	catch (Exception $e)
          	{
          		$error = "File: ".$e->getFile()." Line: ".$e->getLine();
          
          		$this->debug("require", $error, $e->getMessage());
          	}
          

          By putting an "@" before the include, it actually stops any AND I MEAN ALL error reposting from any code in that file and anything that one includes too!

          This is just getting worse and worse...

          I have an idea! - don't use exceptions in PHP!

            By putting an "@" before the include, it actually stops any AND I MEAN ALL error reposting from any code in that file and anything that one includes too!

            I do not recommend using the @ operator at all: as I mentioned earlier, just set display_errors to Off in php.ini on a production environment.

            I have an idea! - don't use exceptions in PHP!

            It's not the fault of exceptions. It is the fault of having to be backward compatible. Exceptions are a PHP 5 feature, but include is a language construct that has been around... forever.

              include()

              should simply return TRUE and false Bool.

              Maybe in php 5.2.7 it would have the option to use it like this, to enable what I'm trying to achieve.

              
              $boolResult = include('whatever.CLASS.php');
              
              if ($boolResult == false)
              {
                  throw new Exception('Include didnt work');
              }
              catch(Exception $e)
              {
                  echo $e->__tostring();
              }
              

              That would be really simple...

              You know, I do understand that php are trying to implement the Exception things, but php4 compatibility makes this impossible.

                Maybe in php 5.2.7 it would have the option to use it like this, to enable what I'm trying to achieve.

                Oh dear, the mention of a future version of PHP made me check up an old link on Minutes PHP Developers Meeting for PHP 6, and the section on All non-fatal errors should become exceptions suggests a current workaround.

                I should have played around with the old set_error_handler() instead of reaching for the newfangled error_get_last().

                EDIT:
                In other words, you can write:

                <?php
                function error_handler($type, $message)
                {
                    throw new Exception($message);
                }
                
                set_error_handler('error_handler');
                
                try
                {
                    include 'whatever.class.php';
                }
                catch (Exception $e)
                {
                    echo $e->getMessage();
                }
                ?>

                  Cool, so what if I'm doing this within a class?

                  Would it look something like this:

                  
                  <?php
                  
                  class whatever
                  {
                  
                  function error_handler($type, $message)
                  {
                      throw new Exception($message);
                  }
                  
                  
                  function __construct()
                  {
                       set_error_handler(self::'error_handler');
                  
                   try
                   {
                       include 'whatever.class.php';
                   }
                   catch (Exception $e)
                   {
                       echo $e->getMessage();
                   }
                  }
                  
                  
                  } // End class
                  ?> 
                  
                  

                  Obviously that won't work, I can see it won't but what should it be?

                    set_error_handler() works globally, so you should use a free function and set it globally.

                      Write a Reply...