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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:21:05+00:00 2026-05-16T08:21:05+00:00

In a lot of my PHP classes, I have this code: private $strError =

  • 0

In a lot of my PHP classes, I have this code:

private $strError = "";
private $intErrorCode = NULL;
private $blnError = FALSE;


public function isError() {
    return $this->blnError;
}

public function getErrorCode() {
    return $this->intErrorCode;
}

private function setError( $strError, $intErrorCode = NULL ) {
    $this->blnError = TRUE;
    $this->intErrorCode = $intErrorCode;
    $this->strError = $strError;
}

The point is so that outside code can know if an object has an error state, what the string of the error is, etc. But to have this exact code in a bunch of different classes is repetitious!

I’d love to have a dual-extension where I could do

class childClass extends parentClass, error {
    ...
}

And have those properties and methods inborn, But PHP doesn’t support multiple inheritances. What I’m thinking about doing is creating an error class that exists inside each class. If I make it public, I can call it directly through the object

if ( $myObject->error->isError() ) {...}

but wouldn’t that also make its error status settable from outside the containing class,

$myObject->error->setError("I shouldn't be doing this here");

which I would rather avoid?

Or I could write ‘gateway’ functions in the containing class, which do the appropriate calls on the error object, and prevent setting the error status from outside,

class childClass extends parentClass {

    private $error;

    public function __construct(...) {
        ...
        $error = & new error();
        ...
    }

    public function isError() {...}
    public function getError() {...}
    public function getErrorCode() {...}
    private function setError() {...}

    ...
}

but that leads to (some of) the code duplication that I’m trying to avoid.

What’s the optimal solution here? I’m trying to have functionality for error statuses for a number of objects, so that the outside world can see their error state, with minimal repetition.

  • 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-16T08:21:06+00:00Added an answer on May 16, 2026 at 8:21 am

    Use composition instead of inheritance.

    class Errors {
    
        private $strError = "";
        private $intErrorCode = NULL;
        private $blnError = FALSE;
    
    
        public function isError() {
            return $this->blnError;
        }
    
        public function getErrorCode() {
            return $this->intErrorCode;
        }
    
        private function setError( $strError, $intErrorCode = NULL ) {
            $this->blnError = TRUE;
            $this->intErrorCode = $intErrorCode;
            $this->strError = $strError;
        }
    
    }
    

    And now use a private instance variable to refer to it:

    class childClass extends parentClass {
        private $errors = new Errors();
        ...
    }
    

    The private visibility prevents you from referencing $errors outside of the class.

    There’s also no need to create isError(), getError(), etc. inside childClass (and therefore no need to worry about code duplication). Simply call $this->errors->isError(), $this->errors->getError(), etc. If you still wanted to require those methods to be implemented though, as suggested below, you could specify an interface.

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

Sidebar

Related Questions

In a lot of my PHP projects, I end up with classes that have
I have quite a lot of PHP view files, which I used to include
I have read a lot of popular standards manuals for open source PHP projects.
I'm working with some old PHP code that has a lot of the following:
I need some advise on my PHP code organisation. I need classes where I
I did a lot of PHP programming in the last years and one thing
A lot of OS projects I know (I am PHP developer) uses versions as
When building some of my PHP apps, a lot of the functionality could be
I'm writing an application in PHP that uses a LOT of global variables that
I'm finding myself doing a lot of things with associative arrays in PHP. I

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.