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

  • Home
  • SEARCH
  • 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 737789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:45:43+00:00 2026-05-14T07:45:43+00:00

I’m writing a web application (PHP) for my friend and have decided to use

  • 0

I’m writing a web application (PHP) for my friend and have decided to use my limited OOP training from Java.

My question is what is the best way to note in my class/application that specific critical things failed without actually breaking my page.

My problem is I have an Object “SummerCamper” which takes a camper_id as it’s argument to load all of the necessary data into the object from the database. Say someone specifies a camper_id in the query string that does not exist, I pass it to my objects constructor and the load fails. I don’t currently see a way for me to just return false from the constructor.

I have read I could possibly do this with Exceptions, throwing an exception if no records are found in the database or if some sort of validation fails on input of the camper_id from the application etc.

However, I have not really found a great way to alert my program that the Object Load has failed. I tried returning false from within the CATCH but the Object still persists in my php page. I do understand I could put a variable $is_valid = false if the load fails and then check the Object using a get method but I think there may be better ways.

What is the best way of achieving the essential termination of an object if a load fails? Should I load data into the object from outside the constructor? Is there some sort of design pattern that I should look into?

Any help would be appreciated.

function __construct($camper_id){
        try{
            $query = "SELECT * FROM campers WHERE camper_id = $camper_id";
            $getResults = mysql_query($query);

            $records = mysql_num_rows($getResults);

            if ($records != 1) {
                throw new Exception('Camper ID not Found.');
            }

            while($row = mysql_fetch_array($getResults))
            {
                $this->camper_id = $row['camper_id'];
                $this->first_name = $row['first_name'];
                $this->last_name = $row['last_name'];
                $this->grade = $row['grade'];
                $this->camper_age = $row['camper_age'];
                $this->camper_gender = $row['gender'];
                $this->return_camper = $row['return_camper'];
            }
        }
        catch(Exception $e){
            return false;
        }



    }
  • 1 1 Answer
  • 0 Views
  • 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-14T07:45:44+00:00Added an answer on May 14, 2026 at 7:45 am

    A constructor in PHP will always return void. This

    public function __construct()
    {
        return FALSE;
    }
    

    will not work. Throwing an Exception in the constructor

    public function __construct($camperId)
    {
        if($camperId === 1) {
            throw new Exception('ID 1 is not in database');
        }
    }
    

    would terminate script execution unless you catch it somewhere

    try { 
        $camper = new SummerCamper(1);
    } catch(Exception $e) {
        $camper = FALSE;
    }
    

    You could move the above code into a static method of SummerCamper to create instances of it instead of using the new keyword (which is common in Java I heard)

    class SummerCamper
    {
        protected function __construct($camperId)
        {
            if($camperId === 1) {
                throw new Exception('ID 1 is not in database');
            }
        }
        public static function create($camperId)
        {
            $camper = FALSE;
            try {
                $camper = new self($camperId);
            } catch(Exception $e) {
                // uncomment if you want PHP to raise a Notice about it
                // trigger_error($e->getMessage(), E_USER_NOTICE);
            }
            return $camper;
        }
    }
    

    This way you could do

    $camper = SummerCamper::create(1);
    

    and get FALSE in $camper when the $camper_id does not exist. Since statics are considered harmful, you might want to use a Factory instead.

    Another option would be to decouple the database access from the SummerCamper altogether. Basically, SummerCamper is an Entity that should only be concerned about SummerCamper things. If you give it knowledge how to persist itself, you are effectively creating an ActiveRecord or RowDataGateway. You could go with a DataMapper approach:

    class SummerCamperMapper
    {
        public function findById($id)
        {
            $camper = FALSE;
            $data = $this->dbAdapter->query('SELECT id, name FROM campers where ?', $id);
            if($data) {
                $camper = new SummerCamper($data);
            }
            return $camper;
        }
    }
    

    and your Entity

    class SummerCamper
    {
        protected $id;
        public function __construct(array $data)
        {
            $this->id = data['id'];
            // other assignments
        }
    }
    

    DataMapper is somewhat more complicated but it gives you decoupled code which is more maintainable and flexible in the end. Have a look around SO, there is a number of questions on these topics.

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

Sidebar

Related Questions

No related questions found

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.