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 1040475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:13:42+00:00 2026-05-16T15:13:42+00:00

I’m transitioning to using OOP for all my projects, historically everything I’ve built has

  • 0

I’m transitioning to using OOP for all my projects, historically everything I’ve built has been pretty small and OOP didn’t seem to be an efficient choice, but now with large projects it is. However more recently I’ve been coming across more and more “best practice” questions that I have and I can’t find the answer for.

For example, imagine I have the following:

class numbers{

    function __construct($number){
        $this->number = (int)$number;
    }

    function add($add){
        $this->added = $this->number + $add;
    }

    function multiply($multiply){
        $this->multiplied = $this->number * $multiply;
    }

    function minus($minus){
        $this->minused = $this->number - $minus;
    }

    function number(){
        return $this->number();
    }
}

Now let’s say that I want to apply add, then multiply and then minus. Each stage can possibly fail (I didn’t include that in the example, but imagine it’s there). This is where my problem lies, should I do:

$numbers = new numbers(8);
if($numbers->add(7)){
    if($numbers->multiply(6)){
        if($numbers->minus(7){
            echo $numbers->number();
        }else{
            echo 'error minusing';
        }   
    }else{
        echo 'error multiplying number';
    }   
}else{
    echo 'error adding number';
}

Or should I have that part in my constructor, like:

class numbers{

    function __construct($number){
        $this->add(6);
        $this->multiply(9);
        $this->minus(7);
        if($this->error){
            return false;
        }else{
            return true;
        }
    }

    function add($add){
        $this->added = $this->number + $add;
        if(!this->added){
            $this->error = "couldn't be added";
        }
    }

    function multiply($multiply){
        $this->multiplied = $this->number * $multiply;
        if(!this->multiplied){
            $this->error = "couldn't be multiplied";
        }
    }

    function minus($minus){
        $this->minused = $this->number - $minus;
        if(!this->minused){
            $this->error = "couldn't be minused";
        }
    }

    function number(){
        return $this->number();
    }

    function error(){
        return $this->error();
    }

}

and then simply do:

$numbers = new numbers(5);
if($numbers){
    echo $numbers->number();
}else{
    echo $numbers->error();
}

Sorry if the example is long winded (also ignore the errors, I wrote it here just to outline what I’m trying to do, this isn’t code I’m using…) but I don’t know how to phrase the question without an example. Basically, should I be doing checking for the errors inside of the class or should I be doing it outside when I call the class?

  • 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-16T15:13:43+00:00Added an answer on May 16, 2026 at 3:13 pm

    It almost always makes sense to enforce consistency on updates inside the class (i.e. any time a method can mutate the internal state of the class, check the input). Then use exceptions to make it the caller’s problem if that input is not well-formed, or if that operation will violate some class invariant.

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

Sidebar

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.