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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:17:16+00:00 2026-05-15T14:17:16+00:00

Say hypothetically I have a class… class Main { $prop1 = 2; $prop2 =

  • 0

Say hypothetically I have a class…

class Main {

    $prop1 = 2;
    $prop2 = 23;
    ...
    $prop42 = "what";

    function __construct($arg_array) {
        foreach ($arg_array as $key => $val) {
            $this->$key = $val;
            }
        }
    }

Say I create and object…


$attributes = array("prop1"=>1, "prop2"=>35235, "prop3"=>"test");
$o = new Main($attributes);

Providing for default property values if not supplied by the user is obvious. But what if I want to enforce arbitrary limits on user supplied values for object properties? What if I want to enforce $prop1 to be of int, be no less than 1, and be no greater than 5. And, $prop42 to be of type string, no less than ‘A’, and no greater than ‘Z’? For this purpose, what would be the cleanest way, keeping the script as short and sweet as possible, using any possible language feature or trick?

I’m stuck in __construct() checking supplied values against a rule array built like so…

$allowable = array(
    "prop1" => array(
        'type' => 'int',
        'allowable_values' => array(
            'min' => 1,
            'max' => 5
            )
        ),
    "prop2" => array(
        'type' => 'int',
        'allowable_values' => array(
            1,
            235,
            37,
            392,
            13,
            409,
            3216
            )
        ),
    ...
    "prop42" => array(
        'type' => 'string',
        'allowable_values' => array(
            'min' => 'A',
            'max' => 'Z'
            )
        )
    );

As you can see by prop2, my validation function is starting to get pretty messy with so many ‘if-then-iterate-again’ blocks as I have to account for not only ranges but a list of permitted values. With the validation code and this rule array, my script is getting rather bulky.

The question is, how can I structure my class or class properties or the validation code or any other aspect of my script to be as short and concise as possible to allow property range and value enforcement? Is there a language feature or trick to handle this more elegantly? Have I reached a brick wall, the limit of this language? Are there any examples from other languages that can easily implement this which can provide some clue?

  • 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-15T14:17:17+00:00Added an answer on May 15, 2026 at 2:17 pm

    I ran into a similar issue the other day. Here’s what I would do:

       private $props;
       private $rules; 
    
       function __construct($params) {
    
          // or you can get the rules from another file, 
          // or a singleton as I suggested
    
          $this->rules = array (array('type' => 'range', 'min' => 10, 'max' => 20), 
            array('type' => 'in_set', 'allowed' => array(1,2,3)));
    
          for ($i=0; $i<count($params); $i++) {
    
             if ($this->check($params[$i], $this->rules($i))
                $this->props[$i] = $params[$i];
             else
                throw new Exception('Error adding prop ' . $i);
          }
    
       }
    
    
       function check($value, $rule) {
          switch($rule['type']) {
             case 'range':
                return ($rule['min'] <= $value && $value <= $rule['max']);  
    
             case 'in_set':
                return (in_array($value, $rule['allowed']));
    
             // and so on
          }
       }
    

    If you have many parameters, you can use an array and iterate through that.
    If your validation rules are always going to be the same, you can put them in a separate file and load that in your constructor or whatever.

    EDIT: By the way, there is really no point in testing type in PHP. It is both not very reliable and unnecessary.

    EDIT 2: Instead of having a global variable with the rules, you can use a Singleton:

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

Sidebar

Related Questions

Let's say that hypothetically I have a table called Products with a primary key
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
Say I have classes class A{ //code for class A } class B{ //code
Let's say I have <parent> <module-prj-1> <module-prj-2> <module-prj-3> Hypothetically, module-prj-1 is a project which
Let's say hypothetically you have a two-level paging system with a 32-bit logical address
My problem is this: Say I hypothetically have a table called fastfood which has
I'm using SQL Server 2008. Let's say I have two hypothetical tables like below:
Let's say, hypothetically (read: I don't think I actually need this, but I am
Say I have a Telerik MVC Grid, AJAX bound and I want to put
Say we have deep hashes like: b = {1 => {2 => {} },

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.