Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

why do I need alias in namespace

<?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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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 '\'.
Avatar of rgb192

ASKER

<?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');
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

thanks for many namespace examples
You are welcome - thanks for the points.