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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:45:59+00:00 2026-05-26T07:45:59+00:00

I’m looking for a solution on how to use argument list for instantiating class’

  • 0

I’m looking for a solution on how to use argument list for instantiating class’ dynamically.

For example:

class test {
    
    public function __call($name, $args){
        /* blah, blah class exists? require */
        return new $name($args);
    }

    public function __toString(){
        return (string) $this->extension(); // to enforce __toString of extension
    }
}

class extension extends test {
    public function __construct(classTypeHint $object, $required, $random = 25){
        // This does not work, results in:
        // "Argument 1 passed to extension::__construct must be an instance of classTypeHint, array given...
    }

    public function __toString(){
        return var_export($this, true);
    }
}

echo new test;

As comment states, that results in error, but I’m looking to be able to access $object as ($args[0]), $required as ($args[1]) etc. Plus, enforce "correct" arguments.

Why am I looking for this?

The __autoload functionality of PHP is limited. Well, maybe it’s not, but I have not found an easy way to differentiate class autoloads.

With this functionality, I can use __call(), __get(), __callStatic() for three different autoloads. Like:

  1. __call() looks for libraries in /includes/libraries
  2. __get() looks for plugins in /application/plugins
  3. __callStatic() looks for, umm, cats in /application/cats

If there is a better way through autoloading, please share. But I’m still looking for such a solution, if it’s possible. Maybe with Reflection classes?

Somewhat working example with ReflectionClass:

$instance = new ReflectionClass($name);
$instance->newInstanceArgs($arguments);
return $instance;

But it’s obvious here, that it will call ReflectionClass->__toString() here. Plus, overlooking the returned dump, it actually does not use the newInstanceArgs() here.

Notes:

I’m interested in example working prior to PHP 5.3.

  • 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-26T07:46:00+00:00Added an answer on May 26, 2026 at 7:46 am

    Use reflection:

    public function __call($name, $args)
    {
        return (new ReflectionClass($name))->newInstanceArgs($args);
    }
    

    it takes an array just like call_user_func_array

    Edit: a working example: http://codepad.org/ZZpBwRij

    <?php
    
    class MyClass
    {
        public function __construct($a, $b)
        {
            echo "__construct($a, $b) called";
        }
    }
    
    function create($name, $args)
    {
        $class = new ReflectionClass($name);
        return $class->newInstanceArgs($args);
    }
    
    var_dump(create('MyClass', array(1, 2)));
    
    ?>
    
    Output:
    
    __construct(1, 2) called
    object(MyClass)#2 (0) {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.