Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1016673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:35:01+00:00 2026-05-16T10:35:01+00:00

I have a conceptual problem to do with PHP classes and error handling.. Below

  • 0

I have a conceptual problem to do with PHP classes and error handling.. Below is a basic class demonstrating my issue.

A function to create a database record exists named “domain_create”. This function calls a secondary function to ensure that the domain to be created does not already exist within the database table.

In the past I have always used true or false to reflect if the function had found a record, but that creates a flaw in my logic.. Records are inserted when the domain_lu function returns false, however should be done if an error is encountered such as the select failing? Returning false would cause the create function to believe nothing has been found and continue with the create process..

My question is how should multiple states be reflected in this scenario? Is there a “best practise” for this scenario?

<?php

require_once('auth.base.class.php');
require_once('mysql.class.php');

class auth extends base
{
   public function __construct()
   {
      parent::__construct();
   }

   /*
    * User
    */

   public function domain_create($args='')
   {
      if ( domain_lu($args['dname']) === FALSE )  
      {
         return $error['Domain already in use'];
      }
   }

   /* 
    * Domain
    */

   private function domain_lu($dname)
   {
      $sql = "SELECT name FROM domain WHERE name = '$dname'";
      $this->_mysql->SQLQuery($sql); 

      if ($this->_mysql->numRow() > 0) return true; 
      else return false;
   }
}

?>
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:35:02+00:00Added an answer on May 16, 2026 at 10:35 am

    You should use Exceptions. If there’s a query error throw an exception stating the reason for failure. If the domain already exists, throw an exception stating that the domain exists already. Here’s a very simple example:

       public function domain_create($args='')
       {
          if (!$this->domain_lu($args['dname']))  
              throw new Exception('domain already in use');
       }
    
       private function domain_lu($dname)
       {
          $sql = "SELECT name FROM domain WHERE name = '$dname'";
          $this->_mysql->SQLQuery($sql); // SQLQuery should throw an exception if it fails
          return ($this->_mysql->numRow() > 0);
       }
    

    You should really create specialized exception classes so user can catch the specific exception without relying on the exception message alone, the PHP docs have more info on that. But basically doing things this way will bail out from the function as soon as an exception is thrown, and go back as long as there’s code that catches the exception. Or if it’s not caught anywhere, PHP will terminate the script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a conceptual problem here, I have like this: abstract class A {
Here is my problem : I have a post controller with the action create.
I have a problem at the conceptual level, I read a lot but I'm
I'm having a conceptual problem. I have this asp.net page to manage code snippets.
This is more of a conceptual problem. I have a server, and I have
I have a conceptual question... I am making an Intranet application (Web platform) for
I think I have a conceptual misunderstanding and would appreciate an explanation. Within a
i have a very conceptual question regarding jquery. i have this fiddle http://jsfiddle.net/zvCrN/ What
More of a design/conceptual question. At work the decision was made to have our
I have a number of classes that are all related conceptually, but some more-so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.