views:

43

answers:

1

Hei all.

i work a lot in PHP5 but i never relly understand the namespace method in PHP can sombady help me here? i have read on php.net's website its not expalde good enof, and i can't find eks. 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

Hobe on help :)

+2  A: 

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();
Michael Clerx
Okai, so i need to use "namespace" before i using items in the namespace?
NeoNmaN
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, :)
NeoNmaN
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";`)
Michael Clerx
Okai, thanks for asware relly use ful :)
NeoNmaN