Object Oriented Programming(OOP) Series: Namespaces

Object Oriented Programming(OOP) Series: Namespaces

Namespaces in simple terms with examples in object oriented programming

Namespaces

Name - Spaces: the space or unique identify between names so they can be readable and don't get to conflict with each other - that's the meaning in programming absolutely.

An ideal situation is where in order to avoid conflicts of students' names they either write their full names with spaces or a naming convention that uniquely identifies each student without conflicts because there's a possibility of having students with same full names in a classroom.

In this OOP concepts series using PHP, Javascript, and Dart we've covered Classes & Objects , Attributes & Methods, Constructor & Destructor, Access modifier, Inheritance, Polymorphism, Abstraction, Encapsulation

When we write our full names for instance Alemoh Rapheal Baja and AlemohRaphealBaja the former is more presentable and readable because it has spaces and each word is distinct.

However, with that meaning in view let's bring it into the context of object-oriented programming.

For example, an object is an instance of a class which can be imported at the top of another file in which it's properties can be accessed. While the application begins to grow there's every tendency that there will be class naming conflicts either within the program, with external packages or some classes might need to have the same name for clarity purposes, to avoid this Namespaces comes into play.

Namespace is the approach of grouping named scope to avoid conflicts with similar names as used in other scopes. Namespace block contains symbols that are grouped into a named scope to avoid conflicts with similarly named symbols in other scopes.

This is the meaning across several object-oriented languages but the implementation varies based on the syntax used.

Prior to this time there's a technique implemented to avoid this for example considering we have a class called Tutorial and we need to reuse this same name within same application by writing it as TutorialTwo or TutorialVariation

You should have also noticed, that in computer filesystem no folder or files can have the same name within the same location/directory. What the OS automatically does is to enforce integers on the name of files or folders if this occurs. For example if a file named tutorial exists on the desktop directory an attempt to name another file tutorial will not be possible the OS alerts the user that there's a name similar to this do you want to override or keep both files. If the user clicks keep both it automatically becomes tutorial and tutorial1

Namespaces enables:

Better organization by grouping classes that work together Same name to be used for more than one class without conflicts

Namespaces in PHP

PHP uses a reserved keyword namespace at the top of the PHP file to make the entire code namespaces.

<?php
namespace Tutorial;

public function oop_series(){}

//the method above belong to Tutorial namespace

?>

It's possible to also have sub namespaces in a scenario where there are different classes under one class

namespace Tutorial\CodePHP;

public function intro_to_php(){}

//the method above belong to CodePHP namespace

?>

Another example---


```python

Practical usage of namespaces in PHP

"; } } function datatypes() { echo "This is an introduction to datattypes in php from CodePHP namespace"; } ?>


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641641328183/ykF-_KMd2.png)

Let's use the namespace in another php file. **lesson.php**

In PHP the **use** keyword is used to import namespace.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641641389527/_AuuODQ1m.png)

**Output**


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641641455931/jFebDc_jZ.png)

**Aliasing Namespaces**

For instance in a case scenario where the path is longer than usual an alias can be used as follows

use Tutorial\CodePHP\Intermediate\API as Api;

//imports the API class directly

$myAPI = new Api();



**Namespaces in Javascript**

Javascript doesn't provide direct namespace implementation or any keyword to be used however there's a way around it to enclose function names inside objects.

Ideally if we export this js file we could access this function in the same way.

const raph = { jsNamespace: () => console.log("Raph a jsNamespace tutor")
}

const alems = { jsNamespace: () => console.log("Alems is a jsNamespace tutor") }

raph.jsNamespace(); alems.jsNamespace();


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1641646079523/rFPeAcwgL.png)

**Namespaces in Dart**

Dart has no direct use of the keyword namespace but has some sort of implementation that resolves conflicts of names.

tutor1.dart

Class Details(){

}

tutor2.dart

Class Details(){

}

import 'package:tutor1/tutor1.dart' as tutor1; import 'package:tutor2/tutor2.dart' as tutor2;

main() { print(new tutor1.Details()); print(new tutor2.Details()); }


**Thank you for reading this article.**

With these topics been treated, you can begin your journey in designing applications using the object-oriented approach in any language of your choice.

Please kindly share with your network and feel free to use the comment section for questions, answers, and contributions.

You love this article?? please follow me on hashnode or twitter @alemsbaja to stay updated for more on these OOP series.

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!