Solved

why do I need alias in namespace

Posted on 2014-04-21
5
151 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 52

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 52

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 52

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now