Namespace
What is Namespace?

If we create same name function in one class.

If we create same class name in one project.

so this will generate fatel error so overcome this problem,we use namespace.

Namespace is a container for class,function and variable. Suppose we need same class name so this is mandatory these class create in different namespace.

Code To Create namespace

<?php
namespace abc{
class xyz{
public  function __construct(){
echo "This the xyz constrctor1";
}
}

$obj=new xyz();
}
?>

This is mandatory you can't create any class above namespace and below namespace

To Create Global namespace

Global namespace is no name

<?php
namespace abc{
class xyz{
public  function __construct(){
echo  "This the xyz constrctor1";
}
}
}

namespace {
class xyz{
public  function __construct(){
echo "This the xyz constrctor2";
}
}

#$obj=new xyz();
$obj=new abcxyz();

}
?>

Laravel Namespace

Be Default Laravel provide us a namespace that is available in appHttpControllers
Here name of namespace is Controllers

Laravel Default Namespace code

<?php

namespace AppHttpControllers;

use IlluminateFoundationBusDispatchesJobs;
use IlluminateRoutingController as BaseController;
use IlluminateFoundationValidationValidatesRequests;
use IlluminateFoundationAuthAccessAuthorizesRequests;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

In the above code Controllers is name of namespace and we write namespace at the top with fullpath.
Below we see keyword use. use doesn't include anything. It just imports the specified namespace.


Previous
Next