Skip to content

Commit

Permalink
Merge pull request #3 from HimalayaMinds/iamyounz-patch-1
Browse files Browse the repository at this point in the history
Add namespace 'MyNamespace' for encapsulating classes, functions, and constants
  • Loading branch information
pradipchaudhary authored Mar 23, 2024
2 parents dd59f50 + f1e130f commit abb3b95
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
# real world example to explain namespaces in PHP

In PHP, a namespace is a way to encapsulate classes, functions, constants, and variables under a specific name. This helps in organizing code and preventing naming conflicts, especially when working with libraries or frameworks that may have similar names for their components.

Here's an example to illustrate the concept of namespaces:

```php
// Define a namespace called 'MyNamespace'
namespace MyNamespace;

// Define a class inside the namespace
class MyClass {
public function __construct() {
echo "This is MyClass inside MyNamespace<br>";
}
}

// Define a function inside the namespace
function myFunction() {
echo "This is myFunction inside MyNamespace<br>";
}

// Define a constant inside the namespace
const MY_CONSTANT = 10;
```

Now, let's use this namespace in another file:

```php
// Include the file containing the namespace
include 'file_containing_namespace.php';

// Create an instance of MyClass using the fully qualified name
$myObject = new \MyNamespace\MyClass();
$myObject->myMethod();

// Call the function using the fully qualified name
\MyNamespace\myFunction();

// Access the constant using the fully qualified name
echo \MyNamespace\MY_CONSTANT;
```

In this example:

- We define a namespace called `MyNamespace`.
- Inside the namespace, we define a class `MyClass`, a function `myFunction`, and a constant `MY_CONSTANT`.
- In another file, we include the file containing the namespace.
- We use the fully qualified name (`\MyNamespace\MyClass`, `\MyNamespace\myFunction`, `\MyNamespace\MY_CONSTANT`) to access the elements of the namespace.

Use cases for namespaces include:

1. **Avoiding Naming Collisions**: If you're using third-party libraries or frameworks, namespaces help prevent naming conflicts between your code and the libraries you're using.
2. **Organizing Code**: Namespaces allow you to organize your code into logical groups, making it easier to manage and maintain.
3. **Autoloading**: Namespaces are often used in conjunction with autoloading, which automatically loads the required class files when they're referenced, improving code efficiency and reducing manual file includes.

By encapsulating code within namespaces, you can write cleaner, more organized PHP applications that are easier to maintain and extend.


Imagine you're developing a web application that includes user authentication functionality. You decide to create a `User` class to handle user-related operations such as login, registration, and profile management. Additionally, you want to use a third-party library called `Mailer` to send email notifications to users.

Expand Down

0 comments on commit abb3b95

Please sign in to comment.