Brandon - Free Palestine
Brandon - Free Palestine

Reputation: 16656

Getting the namespace of the file that called a function in PHP?

If I have the following file:

<?php
namespace SampleProject;

hello_world();

and this in a separate file:

<?php
function hello_world() {

}

Is there anyway for the hello_world() function to get the namespace of the file that called it (So SampleProject)

Upvotes: 3

Views: 166

Answers (1)

cebe
cebe

Reputation: 3819

Depends on what you want to do with that namespace information, but you could add a parameter to the function and then call it like this:

hello_world(__NAMESPACE__);

read more about the __NAMESPACE__ constant in the php documentation.

Adding __NAMESPACE__ inside your function will simply give an empty string since your function is not inside of a namespace.

Upvotes: 2

Related Questions