Mostafa Talebi
Mostafa Talebi

Reputation: 9173

Confusion with PHP Namespace

I'm using php for my new project which is quite large. I feel the need to employ namespaces.

I have read many docs on it, but am confused practically about. Here is the scenario:

I have defined the following namespace and class:

namespace urlNS;

class URL 
{
// content of the class
}

And then in another file, I want to define a class in the same namespace (urlNS) and also extend the URL class (which is a subset of urlNS namespace), and to do this, I have done:

namespace urlNS:

class Inc extends urlNS\url
{
// content 
}

Now, I wanted to make use of a defined namespace twice (or even more), did I do it in a right way?

Upvotes: 0

Views: 52

Answers (1)

barell
barell

Reputation: 1489

Base class definition:

<?php
namespace urlNS;

class URL {}

Subclass:

<?php
namespace urlNS;

class Inc extends URL {}

Using:

<?php
$url = new urlNS\URL();
$suburl = new urlNS\Inc();

Or with use keyword:

<?php
use urlNS\URL;

$url = URL();

Upvotes: 1

Related Questions