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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:00:14+00:00 2026-05-16T01:00:14+00:00

how to get a class constructor function name without instantiating the class? example: $class

  • 0

how to get a class constructor function name without instantiating the class?

example:

$class = 'someClass';
$constructor = somehow get constructor;

$args = array();
$object = call_user_func_array(array($class,$constructor),$args);

what I need is to create a object by passing a undetermined number of variables into it’s constructor.

  • 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-16T01:00:15+00:00Added an answer on May 16, 2026 at 1:00 am

    You can do this with Reflection:

    <?php
    
    class Pants
    {
        public function __construct($a, $b, $c)
        {
            $this->a = $a;
            $this->b = $b;
            $this->c = $c;
        }
    }
    
    $className = 'pants';
    $class = new ReflectionClass($className);
    $obj = $class->newInstanceArgs(array(1, 2, 3));
    var_dump($obj);
    

    This will also work if your constructor uses the old style (unless your code makes use of namespaces and you are using PHP 5.3.3 or, presumably, greater, as old-style constructors will no longer work with namespaced code – more info):

    <?php
    class Pants {
        function Pants($a, $b, $c) { ... }
    }
    

    If the class has no constructor and you wish to use reflection, use $class->newInstance() instead of $class->newInstanceArgs(...). To do this dynamically, it would look like this:

    $object = null === $class->getConstructor() 
        ? $class->newInstance()
        : $class->newInstanceArgs($args)
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to get class object of a generic interface? For example, Boolean.class, Date.class. But
I need to get the class attribute of checked radio button, with name=radio. Used
I need to study how a Constructor of class initializes its object. The key
How can I get the ConstructorInfo for a static constructor? public class MyClass {
I am trying to write a copy constructor for a class but I get
We can get class Class object by 3 methods: MyClass.class obj.getClass Class.forName(className) I don't
I want to get the class name using jQuery And if it has an
Is it possible to get the name of the top level class from an
I have a form without class class ProfilesSearchType extends AbstractType { public function buildForm(FormBuilder
Get class from div inside an li and add to the same li. The

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.