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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:11:54+00:00 2026-06-17T07:11:54+00:00

I have a public method for creating sub-elements in my framework called addElement() which

  • 0

I have a public method for creating sub-elements in my framework called addElement() which is defined as follows:

// addElement - adds an element (experimental version)
public function addElement() {
    if ($arguments = func_get_args()) {
        $class = "\\UI\\{$arguments[0]}";

        if (func_num_args() > 1) {
            $parameters = null;

            foreach (array_slice($arguments, 1) as $argument) {
                $parameters[] = (is_numeric($argument) ? $argument : "\"{$argument}\"");
            }

            $this->elements[($arguments[0] === HTML ? uniqid() : $arguments[1])] = new $class(implode(", ", $parameters));
        }
    }
}

and it gets called like this:

$article1 = new \UI\Article("article1");
$article->addElement(\UI\Aside, "aside1");

or, alternatively (depending on if you need direct access to the new element):

$article1 = new \UI\Article("article1");
$aside1 = $article->addElement(\UI\Aside, "aside1");

The problem comes when I use a method accepting more than two arguments (the type of element and its name, internal-wise), and it’s this:

$article1 = new \UI\Article("article1");
$article1->addElement(\UI\Abbreviation, "abbr1", "RAM", "Random Access Memory");

Using this method, the arguments passed to the function are, literally:

"abbr1", "RAM", "Random Access Memory"

My intention was for this string to be passed like if you were normally passing arguments to a given function. How can I perform that (it’s ok if I need to re-structure the function, although I would ideally like to just add the missing bits, if it’s correct to proceed like this)?

  • 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-06-17T07:11:56+00:00Added an answer on June 17, 2026 at 7:11 am

    You can use reflection when you get an instance of the class, and pass it an array of arguments, like this:

    $ref = new ReflectionClass( $class);
    $this->elements[($arguments[0] === HTML ? uniqid() : $arguments[1])] = $ref->newInstanceArgs( $parameters);
    

    You also don’t need this:

    $parameters = null;
    
    foreach (array_slice($arguments, 1) as $argument) {
         $parameters[] = (is_numeric($argument) ? $argument : "\"{$argument}\"");
    }
    

    You can replace the contents of that if statement with the above code placed after this:

    $parameters = array_slice( $arguments, 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a class that will have one public method, which returns a value
I have class which have one public method Start , one private method and
My dilema is in creating one method that follows the builder pattern, which sets
I am creating a project with a DLL that will have one public method
I have a method: public void StoreNumberInSmallestType(ValueType number) { if (numberTypes == null) numberTypes
the ToSelectList method I have: public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty,
I have this method: public void testJSNI2(){ String x = test; } I can
I have this method: public CampaignCreative GetCampaignCreativeById(int id) { using (var db = GetContext())
I have a class A with a public method in C#. I want to
I have a method that looks like: T[] field; public Method(IList<T> argument) { this.field

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.