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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:51:59+00:00 2026-06-14T06:51:59+00:00

From other posts, it appears that if you have namespaces defined and want to

  • 0

From other posts, it appears that if you have namespaces defined and want to dynamically create an object in another namespace, you have to construct a string and use that in the new call. However, I’m getting a weird behavior. It appears that this method does not work going across namespaces.

User.php:

namespace application\models;

class User {

        public function hello() {
                echo "Hello from User!";
        }
}

Controller.php:

namespace application\controllers;

use application\models;

require('User.php');

$userStr = 'models\\User';
//$userOne = new $userStr();  //Doesn't work. Gets a "Class 'models\User' not found" error
$userOne = new models\User();  //Works fine

$userStr = '\\application\\models\\User';
$userTwo = new $userStr();  //Works fine

$userOne->hello();
$userTwo->hello();

Any idea why when using a variable for the class name, I need to use the fully qualified namespace when it’s in a variable, but hard coded, I can leverage the “use” command?

  • 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-14T06:52:00+00:00Added an answer on June 14, 2026 at 6:52 am

    You can not import with use into variable classnames. That is a limitation of PHP.

    See as well the related questions:

    • Expanding PHP namespace alias to full namespace string
    • Can’t get constant from dynamic class using namespaces
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would love to get some tips from other people that have had this
I've seen some other posts on this, but it appears that when i do
I'm having trouble opening a dijit.Dialog from another dijit.Dialog. I've seen other posts here
I understand from other posts that the only way to handle this is through
As one can glean from other posts , Git's end-of-line normalization has its pros
I'm using code from other c# app and I can see that in WP7
I have another memset question. It appears as if the code I am editing
After reading some posts like this one: Choose File Dialog It appears that Android
I have routes that work perfectly on one machine, but on another machine they
I have a page that display some data. It is loaded from a database

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.