Jump to content

ifubad

Members
  • Posts

    158
  • Joined

  • Last visited

    Never

Posts posted by ifubad

  1. The following interface, because it starts with "interface",  is that considered as a programmer defined interface, and not PHP's predefined interface? Book I am reading is getting me confused. Thank in advance

     

    <?php
    interface Iterator{
        public function current();
        public function key();
        public function next();
        public function rewind();
        public function valid();
    }
    ?>
    

  2. When using multiple catch for custom exceptions, as of now, I have the custom exception classes as separate include files. Like so

     

    Catch block

    catch(FileException $e){
      $e->showError();
    }
    catch(MailException $e){
      $e->logError();
    }
    

     

    custom Exception classes

     

    Filename: FileException.inc.php

    class FileException extends Exception{
      public function showError(){
        die($this->getMessage());
      }
    }
    

     

    Filename: MailException.inc.php

    class MailException extends Exception{
      public function logError(){
        error_log($this->getMessage(),1,'sysadmin@domain.com');
      }
    }
    

     

    Is it possible to keep the different custom exceptions in a single/central file? If so, can you provided a simple example on how to implement. Or, is it that when using multiple custom exceptions, they HAVE to be kept in separate files?

     

    TIA

  3. Got the following first custom Exception example from w3schools. The second example is a slightly modified version, that is not using a custom Exception, and outputs exactly the same message, with slightly less code overall. As stated by somewhere explaining why use a custom exception, because "The default exception class doesn't exactly fulfill the graceful part of handling unpredictable results. It just prints out an error message not much different from regular errors."

     

    So, what is the big advantage of using a custom exception, if the more graceful message can be printed out within the echo statement of the catch block, when using the default Exception class. To produce cleaner code?

     

    Using custom Exception

    <?php
    class customException extends Exception
      {
      public function errorMessage()
        {
        //error message
        $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
        .': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
        return $errorMsg;
        }
      }
    
    $email = "someone@example...com";
    
    try
      {
      //check if
      if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
        {
        //throw exception if email is not valid
        throw new customException($email);
        }
      }
    
    catch (customException $e)
      {
      //display custom message
      echo $e->errorMessage();
      }
    ?>
    

     

    Using default Exception

    <?php
    $email = "someone@example...com";
    
    try
      {
      //check if
      if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
        {
        //throw exception if email is not valid
        throw new Exception($email);
        }
      }
    
    catch (Exception $e)
      {
      //display custom message
      echo 'Error on line '.$e->getLine().' in '.$e->getFile()
        .': <b>'.$e->getMessage().'</b> is not a valid E-Mail address';
      }
    ?>
    

  4. The concept behind this is a centralized point to store variables that contain some globally pertinent information, but not having to change the variable across multiple files.

     

    Ahhh, either an include or ini file, kinda like below(of course the Constants class would actually be in a different file). I guess one can either use a class as below to store the constants, or a simple include with constants created by define(). Which method of storing constants would be the preferred/correct way out of these two, using const in a class or the procedural define()?

     

    For a medium to large site, would storing lots of constants and other pertinent data in an ini file be better than an include?

     

    <?php
    class Constants{
        const QUANTITY = 1;
        const EXAMPLE_CONST = "Blah blah blah";
    }
    
    class ExtException extends Exception{
        private $msg = "Gummy Bear";
    
        public function __construct(){
            $obj = new Constants();
            $msg = Constants::QUANTITY . " " . $this->msg;
            parent::__construct($msg);
        }
    }
    
    try {
        throw new ExtException();
    } catch(ExtException $e) {
        echo $e->getMessage();
    }
    ?>
    

  5. Need a little explanation. Read this snippet in a book. Exactly what is the author suggesting, storing all the constants in what type of file?

     

    Thanks in advance

     

    Were you to develop the MySQL classes further, you might end up with an unwieldy number of constants. In that case it would make sense to remove constant data members from their respective classes and store them in a file associated with the MySQLException class,

  6. The class/book you have found is imposing an artificial restriction that is not present in the mysql_connection function they are claiming to mimic.

     

    I read that section of the book soooo many times and it just kept confusing me. A BIG thank u for the clear explanation, now I can finally move on to the next section , hopefully there will not be any more confusing chapters later.

     

    Thnx a whole bunch, it was driving me nuts.

  7. Sounds like an incorrectly written class.

     

    Thanks for explaining it in a very clear way, it was easily understood.

     

    From what I gathered of what the author of the book is trying to teach, is that the reason he is suggesting to force one open connection at a time only, is mainly to conserve resources. TYPICALLY, does this resource concern justify the author's method, compared to just connecting to multiple dbases simultaneously?

  8. Thank u for the explanation, it cleared things up a most of the way.

     

    Another Question:

    The example from the book is using a connection object, which uses a static variable to programmatically force it to only have one connection at a time. Is it that the main reason is to conserve resources, even though as stated by the above reply, where multiple connections is possible? (these books, sometimes when one keeps reading it over and over, it gets even more confusing)

     

    See book excerpt below.

     

    Book Excerpt:

    Creating a database connection is an expensive operation so restricting creation of connections conserves resources.

     

    By restricting the connection class to a single instance, we are mimicking the built-in mysql_connect function. Its default behavior is to reuse a connection resource rather than create a new one.

     

    However, there are some circumstances where a new connection is a

    necessity.

     

    Two different connection objects are required if a single script needs to connect to two different servers. The close method makes it possible to connect to a different server.

     

    Two instances of the MySQLConnect class can exist, but not simultaneously. If you want to create a connection to another server, you must first close the existing connection. The close method closes the current connection and resets the static variable $instances to 0. Manipulating the $instances variable in this way allows you to create a new connection, but only after the current one is closed.

  9. If I link to a css file, and within that linked css file, it has 3 import css files

     

    @import url(/css/1.css);

    @import url(/css/2.css);

    @import url(/css/3.css);

     

    for the sake of argument, if each import file has the sale rule, but each with its own unique color

     

    Is it correct that the last file "3.css" will override the first two, since that is the last of the import? Just like if using a single css, whatever comes last will override whatever is above it.

  10. if this has to do with a:hover, the IE6 hover bug may require a trigger, like

     

    a:hover {
        font-size: 100%;
    }
    

     

    if font-size does not work, try color:whateverColor, or float:none, or border:none, and so on until you find one that works. The trigger point is different depending on how your overall code is. google for "ie6 hover bug".

  11. odd i clicked it and got nothing like that

     

    can't be 100% sure, but that was the ONLY site (imagebam.com) I checked out that would even pull anything like that.

     

    I google imagebam.com, found this on a yahoo group, happened to others also and imagebam also admitted it, guess it just depends on which ad you just happen to get on the page, and for me, I hit the lotto:

    http://groups.google.com/group/imagebam/browse_thread/thread/bccf7410ea70bf49#

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.