Solved

why do I need alias in namespace

Posted on 2014-04-21
5
148 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 51

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 51

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 51

Expert Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

759 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

18 Experts available now in Live!

Get 1:1 Help Now