?
Solved

why do I need alias in namespace

Posted on 2014-04-21
5
Medium Priority
?
180 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 60

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 60

Accepted Solution

by:
Julian Hansen earned 2000 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 60

Expert Comment

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

829 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