SlideShare uma empresa Scribd logo
1 de 20
PPHHPP NNaammeessppaacceess 
AA ccaassee ssttuuddyy 
By Tamaghna Banerjee
What is Namespace? 
● A class of elements (e.g. addresses, file 
locations, etc.) in which each element has a 
name unique to that class, although it may be 
shared with elements in other classes. 
● In otherways, you could say a namespace 
uniquely identifies a set of names so that there 
is no ambiguity when objects having different 
origins but the same class names are mixed 
together.
Real Scenario – An use case 
● For example, suppose the same XML document included the 
element type of OWNER for owners of motorcycles as well as 
for owners of automobiles. 
● It might be necessary or desirable to know that an owner name 
was one of those who owned a motorcyle rather than an 
automobile. Having different motorcycle and automobile 
namespaces would make this possible. 
● Effectively, it would make it possible to label motorcycle 
owners differently than automobile owners without having to 
create a different element type for each.
What about Namespace in PHP ? 
● In PHP version 5.x namespacing, a new feature known 
was added to the language. 
● Many modern languages like C#, Ruby on Rails already 
had this feature for some time, but PHP was a little late to 
this scene. 
● None the less, every new feature has a purpose, let’s find 
out why PHP namespaces can benefit our application. 
● PHP namespaces allow us to circumvent this issue, in fact 
we can have as many User classes as we like. Not only 
that, but we can use namespaces to contain our similar 
code into neat little packages, or even to show ownership.
Issues with same named Class 
● In PHP you can’t have two classes that share 
the same name. They have to be unique. The 
issue with this restriction is that if you are using 
a third party library which has a class named 
UUsseerr, then you can’t create your own class also 
called UUsseerr. This is a real shame, because that’s 
a pretty convenient class name right?
Let's take a look ahead!! 
● Global Namespace 
Here’s a rreeaallllyy ssiimmppllee ccllaassss..
What sounds like guys? 
● Yeah!!basically, we can think of this class as 
being in the ‘global’ namespace. I don’t know 
if that’s the right term for it, but it sounds 
quite fitting to me. It essentially means that 
the class exists without a namespace. It’s 
just a normal class.
Okk lets play with simple namespace !! 
Simple Namespacing 
Let’s create another class alongside the original, global Eddard. 
Here we have another Eddard class, w ith one 
minor change. The addition of the namespace 
directive. 
The line namespace Stark; informs PHP that 
everything we do is relative to the Stark 
namespace. 
It also means that any classes created within 
this file w ill live inside the ‘Stark’ namespace.
NNooww lleett''ss ddoo aaggaaiinn wwiitthh ''ssttaarrkk'' 
●Now, when we try to use the ‘Stark’ class once again. 
<?php 
// app/routes.php 
$eddard = new Eddard(); 
●Once again, we get an instance of the first class we created in the last 
section. Not the one within the ‘Stark’ namespace. Let’s try to create 
an instance of the ‘Eddard’ within the ‘Stark’ namespace. Finally it will 
look like this.. 
<?php 
// app/routes.php 
$eddard = new Eddard(); 
$eddard = new StarkEddard();
Note it quickly!! 
● I think you are getting me, yes! we can 
instantiate a class within a namespace, 
by prefixing it with the name of the 
namespace, and separating the two with 
a backward () slash. Now we have an 
instance of the ‘Eddard’ class within the 
‘Stark’ namespace. Aren’t we magical?!
What happened to multi-level 
Namespace ? 
You should know that namespaces can have as many 
levels of hierarchy as they need to. 
For example:
The Theory of Relativity 
By adding the namespace directive to the instantiation example, we have moved 
the execution of the PHP script into the ‘Stark’ namespace. Now because we are 
inside the same namespace as the one we put ‘Eddard’ into, this time we receive 
the namespaced ‘Eddard’ class. See how it’s all relative?
A little problem scenario 
Now that we have changed namespace, we have created a little problem. Can 
you guess what it is? How do we instantiate the original ‘Eddard’ class? The one 
not in the namespace. 
Fortunately, PHP has a trick for referring to classes that are located within the 
global namespace, we simply prefix them with a backward () slash. 
Take a note: With the leading backward () slash, PHP 
knows that we are referring to the ‘Eddard’ in the global 
namespace, and instantiates that one.
Imagine a little bit more!! 
● Use your imagine a little. Imagine that we have 
another namespaced class called TullyEdmure. 
Now I want to use this class from within the ‘Stark’ 
framework. How do we do that? 
Again please take a note..we need the prefixing backward slash to 
bring us back to the global namespace, before instantiating a class 
from the ‘Tully’ namespace.
Woww, 'use' saves us!! 
● It could get tiring, referring to classes within other namespaces 
by their full hierarchy each time. Luckily, there’s a nice little 
shortcut we can use. Let’s see it in action. 
Using the 'use' statement, we can bring one class from another namespace, into the current 
namespace. Allowing us to instantiate it by name only. Now don’t ask me why it doesn’t need 
the backward slash prefix, because I just don’t know. This is the only exception that I know 
of. Sorry about that! You can prefix it with a slash if you want to though, you just don’t need 
to.
A new trick, it's kidding me!! 
To make up for that horrible inconsistency, let me show you another neat trick. We 
can give our imported classes little nicknames, like we used to in the PHP playground. 
Let me show you. 
By using the ‘as` keyword, we have given our ‘Tully/Brynden’ class the ‘Blackfish’ 
nickname, allowing us to use the new nickname to identify it within the current 
namespace. Neat trick right?
It's time to play within a namespace 
By giving the ‘Daenerys’ within the 
‘Dothraki’ namespace a nickname of 
‘Khaleesi’. 
we are able to use two ‘Daenerys’ 
classes by name only. Handy right? 
The game is all about avoiding conflicts, 
and grouping things by purpose or 
faction. 
You can use as many classes as you 
need to.
Structure - Namespace 
● Namespaces aren’t just about avoiding conflicts, we can also 
use them for organisation, and for ownership. Let me explain 
with another example. 
● Let’s say I want to create an open source library. I’d love for 
others to use my code, it would be great! 
● The trouble is, I don’t want to cause any problematic class name 
conflicts for the person using my code. That would be terribly 
inconvenient. 
● Here’s how I can avoid causing hassle for the wonderful, open 
source embracing, individual. 
TamaghnaBlogContentPost 
TamaghnaBlogContentPage 
TamaghnaBlogTag 
Here I have used my name, to show that I created the 
original code, and to separate my code from that of the 
person using my library. Inside the base namespace, I 
have created a number of sub-namespaces to organise 
my application by its internal structure.
Namespace - Limitations 
● In truth, I feel a little guilty for calling this sub-heading ‘Limitations’. What I’m 
about to talk about isn’t really a bug. 
● You see, in other languages, namespaces are implemented in a similar way, 
and those other languages provide an additional feature when interacting 
with namespaces. 
● In Java for example, you are able to import a number of classes into the 
current namespace by using the import statement with a wildcard. In Java, 
‘import’ is equivelent to ‘use’, and it uses dots to separate the nested 
namespaces (or packages). Here’s an example. 
Use TamaghnaBlogContentPost 
Use TamaghnaBlogContentPage 
Use TamaghnaBlogTag 
import Tamaghna.blog.*;
My Content Reference 
Namespace - Reference 
( Click the above link to start reading ) 
Thank you, 
Enjoy new learning

Mais conteúdo relacionado

Destaque

Impact Of Ict on libraries
Impact Of Ict on librariesImpact Of Ict on libraries
Impact Of Ict on libraries
Srinivas Sambari
 
Impact of ICT on libraries
Impact of ICT on librariesImpact of ICT on libraries
Impact of ICT on libraries
Asif Waheed
 
Marketing of information services & products
Marketing of information services & productsMarketing of information services & products
Marketing of information services & products
VISHNUMAYA R S
 
Current trends in library science research
Current trends in library science researchCurrent trends in library science research
Current trends in library science research
VISHNUMAYA R S
 
Information storage and retrieval
Information storage and retrievalInformation storage and retrieval
Information storage and retrieval
Sadaf Rafiq
 
What are Information Sources?
What are Information Sources?What are Information Sources?
What are Information Sources?
Johan Koren
 

Destaque (20)

The association for computing machinery
The association for computing machineryThe association for computing machinery
The association for computing machinery
 
Measures of dispersion.. Statistics& Library and information science
Measures of dispersion.. Statistics& Library and information scienceMeasures of dispersion.. Statistics& Library and information science
Measures of dispersion.. Statistics& Library and information science
 
Uniterm indexing
Uniterm indexing Uniterm indexing
Uniterm indexing
 
Boost and SEO
Boost and SEOBoost and SEO
Boost and SEO
 
Impact Of Ict on libraries
Impact Of Ict on librariesImpact Of Ict on libraries
Impact Of Ict on libraries
 
Metadata Standards
Metadata StandardsMetadata Standards
Metadata Standards
 
Open source software
Open source softwareOpen source software
Open source software
 
Library automation
Library automationLibrary automation
Library automation
 
Impact of ICT on libraries
Impact of ICT on librariesImpact of ICT on libraries
Impact of ICT on libraries
 
Internship in library
Internship in libraryInternship in library
Internship in library
 
Dspace software
Dspace softwareDspace software
Dspace software
 
Www and other key terms
Www and other key termsWww and other key terms
Www and other key terms
 
Taylor & francis online ppt
Taylor & francis online pptTaylor & francis online ppt
Taylor & francis online ppt
 
MEDLINE
MEDLINEMEDLINE
MEDLINE
 
Marketing of information services & products
Marketing of information services & productsMarketing of information services & products
Marketing of information services & products
 
Current trends in library science research
Current trends in library science researchCurrent trends in library science research
Current trends in library science research
 
Post coordinate indexing .. Library and information science
Post coordinate indexing .. Library and information sciencePost coordinate indexing .. Library and information science
Post coordinate indexing .. Library and information science
 
Information storage and retrieval
Information storage and retrievalInformation storage and retrieval
Information storage and retrieval
 
What are Information Sources?
What are Information Sources?What are Information Sources?
What are Information Sources?
 
Library Management System PPT
Library Management System PPTLibrary Management System PPT
Library Management System PPT
 

Semelhante a Namespace inPHP

oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
tutorialsruby
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
tutorialsruby
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
ayandoesnotemail
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
Ajax Experience 2009
 

Semelhante a Namespace inPHP (20)

Learning puppet chapter 2
Learning puppet chapter 2Learning puppet chapter 2
Learning puppet chapter 2
 
The "In Action" Pattern for RIA Development
The "In Action" Pattern for RIA DevelopmentThe "In Action" Pattern for RIA Development
The "In Action" Pattern for RIA Development
 
Me, my self and IPython
Me, my self and IPythonMe, my self and IPython
Me, my self and IPython
 
The Bund language
The Bund languageThe Bund language
The Bund language
 
Java packages
Java packagesJava packages
Java packages
 
Introduction to OOP.pptx
Introduction to OOP.pptxIntroduction to OOP.pptx
Introduction to OOP.pptx
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
 
Oop in php tutorial
Oop in php tutorialOop in php tutorial
Oop in php tutorial
 
OOP
OOPOOP
OOP
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
 
Php tips and tricks by omar bin sulaiman
Php tips and tricks by omar bin sulaimanPhp tips and tricks by omar bin sulaiman
Php tips and tricks by omar bin sulaiman
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
ACM init() Day 6
ACM init() Day 6ACM init() Day 6
ACM init() Day 6
 
Oop's in php
Oop's in php Oop's in php
Oop's in php
 
Ruby for .NET developers
Ruby for .NET developersRuby for .NET developers
Ruby for .NET developers
 

Último

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Último (20)

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 

Namespace inPHP

  • 1. PPHHPP NNaammeessppaacceess AA ccaassee ssttuuddyy By Tamaghna Banerjee
  • 2. What is Namespace? ● A class of elements (e.g. addresses, file locations, etc.) in which each element has a name unique to that class, although it may be shared with elements in other classes. ● In otherways, you could say a namespace uniquely identifies a set of names so that there is no ambiguity when objects having different origins but the same class names are mixed together.
  • 3. Real Scenario – An use case ● For example, suppose the same XML document included the element type of OWNER for owners of motorcycles as well as for owners of automobiles. ● It might be necessary or desirable to know that an owner name was one of those who owned a motorcyle rather than an automobile. Having different motorcycle and automobile namespaces would make this possible. ● Effectively, it would make it possible to label motorcycle owners differently than automobile owners without having to create a different element type for each.
  • 4. What about Namespace in PHP ? ● In PHP version 5.x namespacing, a new feature known was added to the language. ● Many modern languages like C#, Ruby on Rails already had this feature for some time, but PHP was a little late to this scene. ● None the less, every new feature has a purpose, let’s find out why PHP namespaces can benefit our application. ● PHP namespaces allow us to circumvent this issue, in fact we can have as many User classes as we like. Not only that, but we can use namespaces to contain our similar code into neat little packages, or even to show ownership.
  • 5. Issues with same named Class ● In PHP you can’t have two classes that share the same name. They have to be unique. The issue with this restriction is that if you are using a third party library which has a class named UUsseerr, then you can’t create your own class also called UUsseerr. This is a real shame, because that’s a pretty convenient class name right?
  • 6. Let's take a look ahead!! ● Global Namespace Here’s a rreeaallllyy ssiimmppllee ccllaassss..
  • 7. What sounds like guys? ● Yeah!!basically, we can think of this class as being in the ‘global’ namespace. I don’t know if that’s the right term for it, but it sounds quite fitting to me. It essentially means that the class exists without a namespace. It’s just a normal class.
  • 8. Okk lets play with simple namespace !! Simple Namespacing Let’s create another class alongside the original, global Eddard. Here we have another Eddard class, w ith one minor change. The addition of the namespace directive. The line namespace Stark; informs PHP that everything we do is relative to the Stark namespace. It also means that any classes created within this file w ill live inside the ‘Stark’ namespace.
  • 9. NNooww lleett''ss ddoo aaggaaiinn wwiitthh ''ssttaarrkk'' ●Now, when we try to use the ‘Stark’ class once again. <?php // app/routes.php $eddard = new Eddard(); ●Once again, we get an instance of the first class we created in the last section. Not the one within the ‘Stark’ namespace. Let’s try to create an instance of the ‘Eddard’ within the ‘Stark’ namespace. Finally it will look like this.. <?php // app/routes.php $eddard = new Eddard(); $eddard = new StarkEddard();
  • 10. Note it quickly!! ● I think you are getting me, yes! we can instantiate a class within a namespace, by prefixing it with the name of the namespace, and separating the two with a backward () slash. Now we have an instance of the ‘Eddard’ class within the ‘Stark’ namespace. Aren’t we magical?!
  • 11. What happened to multi-level Namespace ? You should know that namespaces can have as many levels of hierarchy as they need to. For example:
  • 12. The Theory of Relativity By adding the namespace directive to the instantiation example, we have moved the execution of the PHP script into the ‘Stark’ namespace. Now because we are inside the same namespace as the one we put ‘Eddard’ into, this time we receive the namespaced ‘Eddard’ class. See how it’s all relative?
  • 13. A little problem scenario Now that we have changed namespace, we have created a little problem. Can you guess what it is? How do we instantiate the original ‘Eddard’ class? The one not in the namespace. Fortunately, PHP has a trick for referring to classes that are located within the global namespace, we simply prefix them with a backward () slash. Take a note: With the leading backward () slash, PHP knows that we are referring to the ‘Eddard’ in the global namespace, and instantiates that one.
  • 14. Imagine a little bit more!! ● Use your imagine a little. Imagine that we have another namespaced class called TullyEdmure. Now I want to use this class from within the ‘Stark’ framework. How do we do that? Again please take a note..we need the prefixing backward slash to bring us back to the global namespace, before instantiating a class from the ‘Tully’ namespace.
  • 15. Woww, 'use' saves us!! ● It could get tiring, referring to classes within other namespaces by their full hierarchy each time. Luckily, there’s a nice little shortcut we can use. Let’s see it in action. Using the 'use' statement, we can bring one class from another namespace, into the current namespace. Allowing us to instantiate it by name only. Now don’t ask me why it doesn’t need the backward slash prefix, because I just don’t know. This is the only exception that I know of. Sorry about that! You can prefix it with a slash if you want to though, you just don’t need to.
  • 16. A new trick, it's kidding me!! To make up for that horrible inconsistency, let me show you another neat trick. We can give our imported classes little nicknames, like we used to in the PHP playground. Let me show you. By using the ‘as` keyword, we have given our ‘Tully/Brynden’ class the ‘Blackfish’ nickname, allowing us to use the new nickname to identify it within the current namespace. Neat trick right?
  • 17. It's time to play within a namespace By giving the ‘Daenerys’ within the ‘Dothraki’ namespace a nickname of ‘Khaleesi’. we are able to use two ‘Daenerys’ classes by name only. Handy right? The game is all about avoiding conflicts, and grouping things by purpose or faction. You can use as many classes as you need to.
  • 18. Structure - Namespace ● Namespaces aren’t just about avoiding conflicts, we can also use them for organisation, and for ownership. Let me explain with another example. ● Let’s say I want to create an open source library. I’d love for others to use my code, it would be great! ● The trouble is, I don’t want to cause any problematic class name conflicts for the person using my code. That would be terribly inconvenient. ● Here’s how I can avoid causing hassle for the wonderful, open source embracing, individual. TamaghnaBlogContentPost TamaghnaBlogContentPage TamaghnaBlogTag Here I have used my name, to show that I created the original code, and to separate my code from that of the person using my library. Inside the base namespace, I have created a number of sub-namespaces to organise my application by its internal structure.
  • 19. Namespace - Limitations ● In truth, I feel a little guilty for calling this sub-heading ‘Limitations’. What I’m about to talk about isn’t really a bug. ● You see, in other languages, namespaces are implemented in a similar way, and those other languages provide an additional feature when interacting with namespaces. ● In Java for example, you are able to import a number of classes into the current namespace by using the import statement with a wildcard. In Java, ‘import’ is equivelent to ‘use’, and it uses dots to separate the nested namespaces (or packages). Here’s an example. Use TamaghnaBlogContentPost Use TamaghnaBlogContentPage Use TamaghnaBlogTag import Tamaghna.blog.*;
  • 20. My Content Reference Namespace - Reference ( Click the above link to start reading ) Thank you, Enjoy new learning