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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:57:17+00:00 2026-06-18T06:57:17+00:00

Code speaks better than words: namespaces.php : <?php namespace foo; use foo\models; class factory

  • 0

Code speaks better than words:

namespaces.php:

<?php

namespace foo;

use foo\models;

class factory
{
    public static function create($name)
    {
        /*
         * Note 1: FQN works!
         * return call_user_func("\\foo\\models\\$name::getInstance");
         *
         * Note 2: direct instantiation of relative namespaces works!
         * return models\test::getInstance();
         */

        // Dynamic instantiation of relative namespaces fails: class 'models\test' not found
        return call_user_func("models\\$name::getInstance");
    }
}

namespace foo\models;

class test
{
    public static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = new self;
        }

        return self::$instance;
    }

    public function __construct()
    {
        var_dump($this);
    }
}

namespace_test.php:

<?php

require_once 'namespaces.php';

foo\factory::create('test');

As commented, if I use the full-qualified name inside call_user_func() it works as expected, but if I use relative namespaces it says the class was not found – but direct instantiations works. Am I missing something or its weird by design?

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

    You have to use the fully qualified classname in callbacks.

    See Example #3 call_user_func() using namespace name

    <?php
    
    namespace Foobar;
    
    class Foo {
        static public function test() {
            print "Hello world!\n";
        }
    }
    
    call_user_func(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0
    call_user_func(array(__NAMESPACE__ .'\Foo', 'test')); // As of PHP 5.3.0
    

    I believe this is because call_user_func is a function from the global scope, executing the callback from the global scope as well. In any case, see first sentence.

    Also see the note aboveExample #2 Dynamically accessing namespaced elements which states

    One must use the fully qualified name (class name with namespace prefix).

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

Sidebar

Related Questions

Since code speaks better than words, would you use this: struct StringEvent { const
code speaks a thousand words page.php?id=123 <?php if(is_ajax()){// function that determines whether the request
A line of code speaks a gazillion words so: $foo = false; $bar =
Here is a code fragment that speaks for itself. Thank you! class Foo :
The code speaks for itself, didn't use sort in code because I unknown your
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
Code: <?php //initializing script, do not modify session_start(); define('IN_SCRIPT', true); //so that global.php cannot
An image speaks always more than a ton of text, here's what I'm trying
I am confused with a code which will speaks out the word from an

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.