Oops namespace in drupal 8

Before Going to Drupal 8. letme tell you about namespace

What is namespace ?

My description in oops. its just to solve the conflict between defining same class in an application.

Say for example in your college or workplace  you would have 2 or more  person with the same name. You would end up in confusion or curious to know  which person is that when somebodies talking or gossiping about that name. so to overcome this you people would come up with  some pet name or nick name to them.

Same here.so namespace its just a nickname for a class

let me explain with example

<?php
//Somefile.php

Class A { 

 public static function tryIt() {
 
 echo "no conflict till now ryt?"; 
 
 }

}

A::tryIt();


Till now no conflict

Create another file 

<?php
//Somefile2.php

Class A { 

 public static function tryIt() {
 
 echo "no conflict till now ryt?"; 
 
 }

}

A::tryIt();

 

So Now I'm going to include the second file in first you see any issues

try

Somefile.php

<?php

include "somefile2.php"

Class A { 

 public static function tryIt() {
 
 echo "conflict now ryt? same class defined two times"; 
 
 }

}

A::tryIt();

//PHP Fatal error:  Cannot redeclare class A in /var/www/html/somefile2.php on line 3


 

Now I'm Changing the first file slightly edit the first file

Somefile.php

<?php
namespace Canbeanything;

include "somefile2.php"



Class A { 

 public static function tryIt() {
 
 echo "no conflict now ryt? same class defined two times"; 
 
 }

}

A::tryIt();

//no conflict till now ryt?no conflict now ryt? same class defined two times

 

For Who ever came across java import in php namespace is equivalent to that. 

import java.io.someclass;

Thats it. 

 

Now in drupal 8 Same if you have a look at all the files .

One more thing You could use the namespaced class anywhere by using use keyword

have look at this 

Someotherfile.php

<?php
use Canbeanything\A; // 

B::tryIt();


Now I'm having Another conflict if the same class A is already exist in that someotherfile.php, how would we handle? let see

Someotherfile.php

<?php
use Canbeanything\A as B; // 

B::tryIt();

Class A{
 
  public static function yoyo() {
   echo "done";
  }

}

A::yoyo();

 

So Now Open the drupal 8 Files now You know to access and use the namespaced class.

letsay goto drupal 8 root path and edit the file now

file path is 

core/modules/book/src/BookManager.php
<?php

namespace Drupal\book;

use Drupal\Component\Utility\Unicode;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Template\Attribute;
use Drupal\node\NodeInterface;

Thats it You are done. see you soon with next article. follow us on social networks.