Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP using namespaces

What are namespaces? In their most widely accepted definition, namespaces are a way to encapsulate elements.

In the world of PHP, namespaces are designed to solve two problems that library and application authors encounter when creating reusable code elements such as classes or functions. They were created to avoid naming conflicts with third-party libraries and also to abbreviate long names.

PHP namespaces provide a way to group related classes, interfaces, functions, and constants into a namespace with a unique and distinctive name. This allows multiple libraries and applications to define code elements with the same names without conflicting with each other.

Sintaxis para definir un namespace
<?php
   namespace namespace_name;
   /*todo lo que este luego de la declaracion estara en este namespace*/
?>

In addition to this syntax, we can define multiple namespaces in the same file.

más de un namespace en un archivo
<?php
   namespace namespace_name;
   /*bloque de code*/
   namespace namespace_name2;
   /*bloque de codigo*/
?>

In the world of PHP, namespaces are often used in conjunction with classes to create organized and maintainable code. For example, in an MVC architecture, namespaces can be used to group related classes together under a common namespace, such as ‘App\Models’ for models.

In some PHP frameworks, like Laravel, the use of namespaces is facilitated by Composer Autoload, which automatically loads PHP classes without the need for manual includes. In these cases, developers often use ‘use’ to import a class or namespace into the current scope without including the file.

It’s worth noting that when using ‘use’, the imported class or namespace can be referred to by its short name (i.e. without the namespace prefix) within the current scope. However, if there are conflicting class or function names within different namespaces, the fully qualified name (i.e. including the namespace prefix) must be used to avoid ambiguity.

Overall, namespaces and ‘use’ statements are powerful tools for creating organized, reusable code in PHP, and are commonly used in modern PHP development

Ejemplo simple de unnamespace
<?php
namespace Salado;
class Ca {
    function __construct(){
    }
    public function saluda(){
        echo "hola";
    }
}
?>

The keyword ‘use’ in Namespaces

When using ‘use’, we can give an alias to the namespace or class being imported. This is particularly useful if our namespace has a long path, such as ‘namespace/subnamespace/sub../sub../class’.

However, if there are few subdirectories, we can simply use ‘new \namespace_path\ClassName’ instead of ‘use’

Utilizando la clase Ca
<?php

namespace Salado;
include("Ca.php");
use Salado\Ca as C;

$salado = new C;
$salado->saluda();
?>

Global namespace

Without any namespace definition, all class and function definitions are placed in the global namespace, as if they were defined before PHP supported namespaces.

Prefixing a name with \ specifies that the name is required from the global namespace even in the context of the namespace.

This means that we can either define global functions or refer to things in the global scope while inside a namespace using .

This can be seen in Laravel and other frameworks where some programmers use the slash to achieve particular things.

References: php.net

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category