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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:29:13+00:00 2026-05-17T18:29:13+00:00

I work a lot in PHP but I never really understand the namespace method

  • 0

I work a lot in PHP but I never really understand the namespace method in PHP. Can somebody help me here? I have read on php.net’s website its not explained good enough, and I can’t find examples on it.

I need to know how I can make code in sample version.

  • namespace: sample
    • class: sample_class_1
      • function: test_func_1
    • class: sample_class_2
      • function: test_func_2
      • function: test_func_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-17T18:29:13+00:00Added an answer on May 17, 2026 at 6:29 pm

    Like this?

    <?php
    
    namespace sample
    {
        class Sample_class_1
        {
            public function test_func_1($text)
            {
                echo $text;
            }
        }
    
        class Sample_class_2
        {
            public static function test_func_2()
            {
                $c = new Sample_class_1();
                $c->test_func_1("func 2<br />");
            }
    
            public static function test_func_3()
            {
                $c = new Sample_class_1();
                $c->test_func_1("func 3<br />");
            }
        }
    }
    
    // Now entering the root namespace...
    //  (You only need to do this if you've already used a different
    //   namespace in the same file)
    namespace
    {
        // Directly addressing a class
        $c = new sample\Sample_class_1();
        $c->test_func_1("Hello world<br />");
    
        // Directly addressing a class's static methods
        sample\Sample_class_2::test_func_2();
    
        // Importing a class into the current namespace
        use sample\Sample_class_2;
        sample\Sample_class_2::test_func_3();
    }
    
    // Now entering yet another namespace
    namespace sample2
    {
        // Directly addressing a class
        $c = new sample\Sample_class_1();
        $c->test_func_1("Hello world<br />");
    
        // Directly addressing a class's static methods
        sample\Sample_class_2::test_func_2();
    
        // Importing a class into the current namespace
        use sample\Sample_class_2;
        sample\Sample_class_2::test_func_3();
    }
    

    If you’re in another file you don’t need to call namespace { to enter the root namespace. So imagine the code below is another file “ns2.php” while the original code was in “ns1.php”:

    // Include the other file
    include("ns1.php");
    
    // No "namespace" directive was used, so we're in the root namespace.
    
    // Directly addressing a class
    $c = new sample\Sample_class_1();
    $c->test_func_1("Hello world<br />");
    
    // Directly addressing a class's static methods
    sample\Sample_class_2::test_func_2();
    
    // Importing a class into the current namespace
    use sample\Sample_class_2;
    sample\Sample_class_2::test_func_3();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's an open ended question. I work on a lot of mssql files, and
I work a lot with network and serial communications software, so it is often
I work a lot with decision makers looking to use technology better in their
I work a lot with external Web Services, and I need to create an
I work a lot with serial communications with a variety of devices, and so
I work with a lot of offsite developers and contractors. I ask them daily
I work with a lot of web applications that are driven by databases of
I work on a lot of Asian Language localization projects, and am looking for
I use eclipse for quite a lot of work, including: multiple utility projects that
A few years ago I did a lot of work with CGI.pm. I'm evaluating

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.