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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:26:54+00:00 2026-05-26T16:26:54+00:00

I have this method : public function getInstance() { $classname = $this->getFullyQualifiedClassName(); return new

  • 0

I have this method :

public function getInstance()
{
    $classname = $this->getFullyQualifiedClassName();
    return new $classname();
}

Is there a way to write this without the $classname variable, which gets used only once?

  • 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-26T16:26:55+00:00Added an answer on May 26, 2026 at 4:26 pm

    You could use Reflection:

    $reflectionClass = new ReflectionClass('SomeClassName'); 
    $reflectionClass->newInstanceArgs(array(1, 2, 3));
    

    But as far as I know, that will be a lot slower than the solution you already have. In a perfect world you could just write something like this:

    return new ( $this->getFullyQualifiedClassName() )(); // THIS DOES NOT WORK!!!
    

    But unfortunately PHP’s syntax isn’t very flexible, and therefore you have to create that ugly variable thats troubling you.


    I just realized that there is another (very ugly) way to do it: eval:

    return eval("return new ".$this->getFullyQualifiedClassName()."();");
    

    I do not suggest that you used this though, because of the security risk that comes with using eval.

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

Sidebar

Related Questions

I have this method in my db class public function query($queryString) { if (!$this->_connected)
I have this controller method: public JsonResult List(int number) { var list = new
I have this method: public function activation_code() { //activation code sent into db $activation_code
I currently have this method: public function getUser($id) { $user = $this->db->getSingleRecord(SELECT user_id, user_login,
I have this method: public bool CanExecute() And after 70 commits, I added an
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
I have this method in cs page: public String getToolTip(Object productId, Object imgBtnId) {
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException
I have a page with this method in CreateTicket.aspx.cs: [WebMethod()] public static string Categories()

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.