3

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 Answer 1

4

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Okai, so i need to use "namespace" before i using items in the namespace?
i ask becures i want the namespace call indsite a class i have right now when i need to use this namespace like a fremawork, :)
The way it works is, every class (and function, and variable) lives inside a namespace. If you don't use the "namespace" keyword it assumes you're in the root namespace. If you want to use items from a different namespace (for example a class from "sample" when you're in "sample_2" or in the root) you have to either address it by its full name (for example sample\Sample_class_1) or import it into your current namespace (using use "sample\Sample_class_1";)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.