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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:01:37+00:00 2026-05-28T08:01:37+00:00

Well, I have a configuration object that contains the settings for various kinds of

  • 0

Well, I have a configuration object that contains the settings for various kinds of objects. My classes are generated at runtime as soon as i want to intantiate a new object of a class that hasn’t been created so, they rely on the config object to know what attributes are mandatory and some other info:
think of it kinda like this:

Config
   ->Class1
      ->Attributes
           ->id
              ->Mandatory: true
           ->imagesource
              ->Mandatory: true
           .................
   ->Class2
     ..........................   

i have a method, validateObject() that checks if the mandatory values are all set for the object, like:

function validateObject($object){
   $config = configObject[get_class($object)];
   foreach($config->attributes as $attrName => $attrVal){
      if($attrVal->mandatory == true){
         if(!isset($object->$attrName){
            throw Error();
         }
      }
   }
}

So far, so good. It happens that now, my config object will have the attribute names turned into camelCase. I decided to let the programmer use any casing they wish so, in the end, i just want to check if the object has any property that, turned to lowercase, will match the one on the Config turned to lowercase to.

My current solution would be something like

function validateObject($object){
   $config = configObject[get_class($object)];
   foreach($config->attributes as $attrName => $attrVal){
      if($attrVal->mandatory == true){
         $lowercaseAttr = str_to_lower($attrName);
         foreach($object as $key => $value){
           if(str_to_lower($key) == $lowercaseAttr){
              //don't throw, move to the next attribute in config
           }
         }
      }
   }
}

This would do the job, although it would need a bit more work. I was looking for a more elegant solution…

I hope i’m not being to confusing, thanks for your help

  • 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-28T08:01:38+00:00Added an answer on May 28, 2026 at 8:01 am

    The only way I could think of doing this is to add a function to your class similar to

      public function check($p){
        $props =get_object_vars($this);
        $array_to_search;
        foreach($props as $key=>$prop){
            $array_to_search[strtolower($key)] = strtolower($key);
        }
        echo "prop :".array_key_exists(strtolower($p),$array_to_search);
    }
    

    and then call

      $obj->check("thingtwo");
    

    The array bit could probably be optimized, I just put this together quickly to see if it would work for you. If you had a number of classes then this function could sit separate and you could pass in an object

      function check($obj,$p){
        $props =get_object_vars($obj);
        $array_to_search;
        foreach($props as $key=>$prop){
            $array_to_search[strtolower($key)] = strtolower($key);
        }
        echo "prop :".array_key_exists(strtolower($p),$array_to_search);
    }
    
        check($myobj, "propertyname");
    

    The above will return true if the property exists but is null. You can check the value by changing the function to

     function check($obj,$p){
        $props =get_object_vars($obj);
        $array_to_search;
        foreach($props as $key=>$prop){
            $array_to_search[strtolower($key)] = $prop;
        }
        echo "prop :".isset($array_to_search[strtolower($p)]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a State class that includes configuration as well as some state-specific methods.
I have a class ShipmentsCollection that inherits ObservableCollection which contains shipment objects (entities). This
well i have a configuration like this in the components part of my config
Well i have a div tag that i show as a modal dialog this
well I have that problem, im using a lightbox srcipt and im opening an
Scenario: I have an application that pulls data from a SQL database as well
For my app, I have a fairly complex set of configuration options that user
I have a web project that uses a custom configuration class to store app
I have a dialog box that the user inserts various data through gui controls,
I have a param Map which can contain strings as well as custom object

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.