Solved

why do I need alias in namespace

Posted on 2014-04-21
5
153 Views
Last Modified: 2014-04-22
<?php

namespace {

function replaceUnderscores( $classname ) {
    //print "underscores\n";
    $path = str_replace('_', DIRECTORY_SEPARATOR, $classname );
    if ( file_exists( "{$path}.php" ) ) {
        require_once( "{$path}.php" );
    }
}

function myNamespaceAutoload( $path ) {
    //print "namespace\n";
    if ( preg_match( '/\\\\/', $path ) ) {
        $path = str_replace('\\', DIRECTORY_SEPARATOR, $path );
    } 
    if ( file_exists( "{$path}.php" ) ) {
        require_once( "{$path}.php" );
    }
}

spl_autoload_register( 'replaceUnderscores' );
spl_autoload_register( 'myNamespaceAutoload' );

$x = new ShopProduct();
$y = new business_ShopProduct();
$z = new business\ShopProduct2();
$a = new \business\ShopProduct3();
}


namespace business {
//$a = new \business\ShopProduct3();
$a = new ShopProduct3();
$b= new ShopProduct2();
}
namespace pants {
    use business\ShopProduct2 as tweetiepie;
    $a = new tweetiepie();
    use business\shopproduct3 as another;
    $a= new another();
}
namespace doesthisneedtobecalled{
  use business\ShopProduct2 as anothercall;
  $a=new anothercall();
}
?>

Open in new window



in
namespace pants
namespace doesthisneedtobecalled
why do I need to use alias to call shopproduct2

$a=new shopproduct2();
is not recognized by my code ide in the last two namespaces

why can $a=new shopproduct2(); in namespace business with no alias
0
Comment
Question by:rgb192
  • 3
  • 2
5 Comments
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 40014283
Consider the following simple example
<?php
namespace {
  class fred{
    function __construct($who)
    {
     echo "Hullo $who I am fred";
    }
  }
  
  $x = new fred('inside');
}
namespace business {
// $x = new fred('outside'); // FAIL
$x = new \fred('outside');
}
?>

Open in new window

Firstly, a namespace is a way of encapsulating functionality under a common name - in the same way as a class encapsulates methods and properties. You cannot invoke a method in a class without an object or a class prefix - the same applies for namespaces.

In the above example fred() is local to the default namespace and so the instantiation of a new object fred() inside the namespace works because the instantiation and declaration of fred are both local to that namespace.

In the second namespace (business) the first instantiation fails because the interpreter does not know where to find the class fred - it looks locally in the namespace and does not find the class so it generates an error.

To get to fred we have to include the namespace - in this case the default namespace which is just a '\'.
0
 

Author Comment

by:rgb192
ID: 40014761
<?php
namespace {
  class fred{
    function __construct($who)
    {
     echo "Hullo $who I am fred";
    }
  }
  
  $x = new fred('inside');
}
namespace business {
 //$x = new fred('outside'); // FAIL
 $x = new \business\fred('outside');
 $x = new \business\('outside');
$x = new \fred('outside');
}
?>

Open in new window


\ takes to default namespace
but I do not understand
what is default namespace if there are many namespace
and why these do not work
 $x = new \business\fred('outside');
 $x = new \business\('outside');
0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40014825
Default namespace is the unamed namespace - same as if you did not include a namespace.

When you say $x = new \business\fred('outside'); does not work - what is the context?
Consider the following example
<?php
require_once('tasks/business.php');
class fred
{
  function __construct($who)
  {
    echo "Hullo $who I am fred";
  }
}

$x = new fred('inside');
$y = new \business\monkey('default');
?>

Open in new window

tasks/business.php
<?php
namespace business {
  class monkey
  {
    function __construct($who)
    {
      echo "Hello $who from monkey";
    }
  }
}
?>

Open in new window

This works - how is your code different?
0
 

Author Closing Comment

by:rgb192
ID: 40015372
thanks for many namespace examples
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 40015480
You are welcome - thanks for the points.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question