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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:04:04+00:00 2026-05-19T01:04:04+00:00

Writing my first PHP Class and came across an issue, the following is my

  • 0

Writing my first PHP Class and came across an issue, the following is my __construct method:

public function __construct($foo,$bar) {
       $this->foo = $foo;
       $this->bar = $bar;
}

Both $foo and $bar are required, without them, the methods will not work. It’s OK when they aren’t defined when the object is instantiated like so:

$var = new Class();

As this throws an exception (e.g. Class requires 2 params, none set). But if they are set but not of the correct type like so:

$var = new Class('33','ddd');

My methods will fail as the variables have the wrong type.

Where should I be validating these? In the constructor or in each method?

My solution that I’m using right now works, but I’m not sure if it’s the right way:

// $foo needs to be a string with letters only
// $bar needs to be an integer
public function __construct($foo,$bar) {
       $this->foo = $foo;
       $this->bar = $bar;
       if(!is_numeric($bar)){
           // Throw exception
       }
       elseif(other validation case)
       etc...
}

The concepts of OO programming are pretty new to me, so links to any reference material you have would be greatly appreciated.

  • 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-19T01:04:05+00:00Added an answer on May 19, 2026 at 1:04 am

    I’d probably do something like this to prevent clutter inside the ctor and to allow the class to set them values internally:

    class MyClass …
    
        protected $_foo;
    
        /**
         * @param  String   $foo    String with letters only
         * @param  Integer  $bar    Any Integer
         * @return void
         * @throws InvalidArgumentException when $foo is not letters only
         * @throws InvalidArgumentException when $bar is not an Integer
         */
        public function __construct($foo, $bar) 
        {
            $this->_setFoo($foo);
            $this->_setBar($bar)
        }
    
        /**
         * @param  String   $foo    String with letters only
         * @return void
         * @throws InvalidArgumentException when String is not letters only
         */
        protected function _setFoo($foo)
        {
            if (FALSE === $this->_consistsOfLettersOnly($foo)) {
                throw new InvalidArgumentException(
                    '$foo should consists of letters only'
                );
            }
            $this->_foo = $foo;
        }  
    
        …
    

    This has the added advantage that if you need to publicly expose the setters later, you just have to change the visibility keyword.

    Making the validation into it’s own method is not strictly necessary, but I think it makes the code more readable. If you find another property in that class needs the same validation, you can also reuse it more easily.

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

Sidebar

Related Questions

This is my first question :). Im writing a little twitter app in PHP
I'm writing my first php page for class. The page is supposed to take
I am currently writing my truly first PHP Application and i would like to
I will be writing my first daemon in php and I have a couple
I'm writing my first Rails app and I'm confused by a method call that
I'm writing a simple linear linked list implementation in PHP. This is basically just
I have the following PHP code; <?php component_customer_init(); component_customer_go(); function component_customer_init() { $customer =
I'm writing my first wordpress plugin and I'm trying to create a function to
Im writing my first online shop in PHP. Now working on add product form
Basically I have two ways for writing something First: <?php echo link_to( static_image_tag( $object->getImageWebPath(),

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.