Results 1 to 6 of 6

Thread: How to Define Namespaces in PHP?

  1. #1
    Join Date
    Jul 2006
    Posts
    182

    How to Define Namespaces in PHP?

    Hi friends,
    Since I am new to the PHP programming language I don't have enough knowledge about it. I want to know about the namespaces that are used in PHP. So, thought that posting here would be helpful. Please tell me how to Define Namespaces in PHP? Any other details regarding the namespaces would also be of great help. Hoping the needful help sooner.!!
    "Yea though I walk through the valley of the shadow of death... I will fear no evil." -Psalms 23

    K8N Diamond Plus (BIOS v1.2)
    AMD Athlon 64 X2 4400+
    Antec TruControl 550W
    NVidia GeForce 7900GT (NGO v1.8466 BETA)
    OCZ Platinum 2x1GB (2-3-2-5)
    SATA: WD740
    PATA: 2xWD2500, WD1200, NEC DVD/RW

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to Define Namespaces in PHP?

    Declaring a namespace in PHP is very simple. You have to declare the namespace by using the 'namespace' keyword. If you are familiar with the C# and Java programming languages, then you can understand quickly. By using the namespaces you can change the structure of PHP applications for the better. Namespace allows the developer to seperate their code into modules or groups which inturn makes the code easier to read. A file containing a namespace must declare the namespace at the top of the file before any other code.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to Define Namespaces in PHP?

    Namespaced code is defined using a single namespace keyword at the top of your PHP file. It must be the first command and it should not contain non-PHP code, HTML, or white-space can precede the command. Also as said before that namespace must declared at the top before any other code. This should pass one exception which is the declare keyword. The only code construct allowed before a namespace declaration is the declare statement, for defining encoding of a source file. The advantage of using the namespaces are that they prevent class and function name conflicts, it allows you to have numerous classes with the same name, as long as they are in different namespaces.

  4. #4
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to Define Namespaces in PHP?

    You can declare a single namespace with the declare exception. You can have a look at the following coding which demonstrates the same :
    PHP Code:
    <?php
    namespace MyExample;

    const 
    CONNECT_OK 1;
    class 
    Connection /* ... */ }
    function 
    connect() { /* ... */  }

    ?>

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to Define Namespaces in PHP?

    While knowing about the namespaces, you should also know about the Sub-namespaces. In PHP, you can define a hierarchy of namespaces so libraries can be sub-divided. You will have to use backslash (\) for separating the sub-namespaces. Just check the example so that you can understand more clearly.
    • MyExample\SubName
    • MyExample\Database\MySQL
    • CompanyName\MyExample\Library\Common\Widget1

    Hope that this post was somewhat useful for you.!!

  6. #6
    Join Date
    Jul 2006
    Posts
    289

    Re: How to Define Namespaces in PHP?

    The following example can explain you more properly. So have a glance at the following example :
    coding of myExample.php
    PHP Code:
    namspace lib;

    class 
    myExample
    {

    public function 
    call()
    {
    echo 
    'hello world';
    }


    And the following is the coding of index.php :
    PHP Code:
    require 'lib/myExample.php';

    $bar = new myExample();

    $bar = new lib\myExample();
    $bar->call();
    use 
    lib\myExample;

    $bar = new myExample();
    $bar->call();

    use 
    lib\myExample as newName;

    $bar = new newName();
    $bar->call(); 
    Signatures reduce available bandwidth

Similar Threads

  1. Namespaces in C#
    By GOOL in forum Software Development
    Replies: 4
    Last Post: 25-12-2010, 06:03 AM
  2. How to use namespaces : fallback to global function in PHP?
    By Nathen in forum Software Development
    Replies: 3
    Last Post: 21-02-2010, 05:05 AM
  3. How to use namespaces : Aliasing/Importing?
    By Bigga Lexx in forum Software Development
    Replies: 4
    Last Post: 21-02-2010, 04:05 AM
  4. How to define Union?
    By seema_thk in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 02:07 PM
  5. Equivalent of #define in C#
    By Luis234 in forum Software Development
    Replies: 6
    Last Post: 20-04-2009, 11:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,876,239.71060 seconds with 17 queries