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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:56:40+00:00 2026-05-15T06:56:40+00:00

So I have a couple of arrays $array_1 = Array(‘one’,’two’,’three’); $array_2 = Array(‘red’,’blue’,’green’); Is

  • 0

So I have a couple of arrays

$array_1 = Array('one','two','three');
$array_2 = Array('red','blue','green');

Is there a dynamic way to create the Setters and Getters for an array with single value entries?

So the class would be something like:

class xFromArray() {

}

So the above if I passed $array_1 it would generate something like this:

private $one;

setOne($x) {
   $one = $x;
}

getOne() {
   return $one;
}

if I passed $array_2 it would generate something like this:

private $red;

setRed($x) {
   $red = $x;
}

getRed() {
   return $red;
}

So I would call it somehow like this? (My best guess but doesn’t seem that this would work)

$xFromArray = new xFromArray;
foreach($array_1 as $key=>$data) {
   $xFromArray->create_function(set.ucfirst($data)($data));
   echo $xFromArray->create_function(get.ucfirst($data));
}
  • 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-15T06:56:41+00:00Added an answer on May 15, 2026 at 6:56 am

    You can use __call() to invoke dynamic methods. So:

    class Wrapper {
      private $properties;
    
      public function __construct(array $names) {
        $this->properties = array_combine(array_keys($names),
          array_fill(0, count($names), null));
      }
    
      public function __call($name, $args) {
        if (preg_match('!(get|set)(\w+)!', $name, $match)) {
          $prop = lcfirst($match[2]);
          if ($match[1] == 'get') {
            if (count($args) != 0) {
              throw new Exception("Method '$name' expected 0 arguments, got " . count($args));
            }
            return $properties[$prop];
          } else {
            if (count($args) != 1) {
              throw new Exception("Method '$name' expected 1 argument, got " . count($args));
            }
            $properties[$prop] = $args[0];
          }
        } else {
          throw new Exception("Unknown method $name");
        }
      }
    }
    

    Personally I wouldn’t go the route of using getters and setters in PHP. Use the special methods __get() and __set() instead and treat these dynamic properties as object properties rather than adding a (most likely unnecessary) method wrapper.

    Edit: to clarify, __call() is invoked when you call an method in an object that either doesn’t exist or is inaccessible. So:

    $wrapper = new Wrapper($array_1);
    $wrapper->setOne("foo");
    echo $wrapper->getOne(); // foo
    $wrapper->getAbc(); // exception, property doesn't exist
    

    __call() is used here to decipher the method name. If it fits the pattern of get or set followed by a property name (from the initial array) then it works as expected, otherwise it throws an exception. You can of course change this behaviour any way you wish.

    See Overloading from the PHP manual for a more detailed explanation of these “magic” methods.

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

Sidebar

Ask A Question

Stats

  • Questions 461k
  • Answers 461k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is a common bug/behaviour of Eclipse. To eliminate it… May 16, 2026 at 12:12 am
  • Editorial Team
    Editorial Team added an answer To add to Lucas's answer: it's very hard to come… May 16, 2026 at 12:12 am
  • Editorial Team
    Editorial Team added an answer The path you gave for the Users OU is not… May 16, 2026 at 12:12 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.