Mr. Jo
Mr. Jo

Reputation: 5271

How should I name my namespaces in PHP correctly?

I'm developing classes in PHP. For naming I followed the PSR-4 Autoloader rules:

https://www.php-fig.org/psr/psr-4/

But somehow I'm confused. So for example if I have this class in the following structure, how should I define the namespace?

The path of the file:

wmx/includes/classes/admin/Wmx_Settings.php

The class name:

class Wmx_Settings {....

My thoughts about namespaces:

namespace Johnny\Wmx\Admin\;
namespace Johnny\Wmx\Admin\Wmx_Settings;

So how should I define my namespaces now? I'm not very clear about that. Thanks for your help!

Upvotes: 0

Views: 145

Answers (2)

Muzammal Hussain
Muzammal Hussain

Reputation: 1

namespace MyNamespaceName; 
    
require 'project/database/connection.php'; 
    
use Project\Database\Connection as Connection; 
    
$connection = new Connection(); 
    
use Project\Database as Database; 
    
$connection = new Database\Connection(); 

Upvotes: -1

Kongulov
Kongulov

Reputation: 1171

use

namespace Johnny\Wmx\Admin;
...
class Wmx_Settings {....

Upvotes: 3

Related Questions