0

I would like to "namespace" a collection of functions and call them. The reason is that I'd like to keep the function names the same across multiple files (and thus distinguish them by namespace). My below implementation yields a "Call to undefined function..." error:

main.php:

//located in /src/main.php 
namespace Roles;
use functions\Form; //doesn't seem to work
use functions\View; //doesn't seem to work
use Silex\Application; //works 
use Silex\ControllerProviderInterface; //works

class mainfct {
//calls respective functions in the namespaces
  Form\a();
  Form\b();

  View\a();
  View\b();
}

form.php:

//located in /src/functions/form.php
namespace Form;
function main() { a(); b();};
function a() {...};
function b() {...};

view.php:

//located in /src/functions/view.php
namespace View;
function main() { a(); b();};
function a() {...not necessarily same code as above...};
function b() {...not necessarily same code...};

The above doesn't seem to work. What's the best way to implement this?

fibono
  • 773
  • 2
  • 10
  • 23
  • Can you explain what `The above doesn't seem to work.` means? We don't have a glass globe to find that out. Do you get any error? – Charlotte Dunois Aug 12 '15 at 16:06
  • "Call to undefined function" error. Theoretically is it supposed to work? – fibono Aug 12 '15 at 16:10
  • Do you use `use {namespace name here};` somewhere before? It could be you're not on the global namespace anymore. Try with prepending a backslash before the namespace name. – Charlotte Dunois Aug 12 '15 at 16:11
  • This is better supported in PHP 5.6 http://php.net/manual/en/language.namespaces.importing.php. `use function ...`. Im not sure if that is what you are looking for though. – Flosculus Aug 12 '15 at 16:12
  • I edited my post to include more information. Hopefully it's more clear. – fibono Aug 12 '15 at 16:37
  • 1
    Seems like these would be better suited as a class or group of, then you can use inheritance and keep it DRY. Whats the diffrence between `$Form->b()` and `$View->b()` instead of `\Form\b()` and `\View\b()`. If you don't want the instance of then make them static `Form::b()` `View::b()`. – ArtisticPhoenix Aug 12 '15 at 16:54

3 Answers3

3

It sounds like you're not using PHP 5.6 -- prior to that importing of functions or constants (view use) was unimplemented in the language.

Re: your update -- unlike the standard included library in languages like ruby or python, PHP namespaces are not tied to the filename/path. You're trying to import functions\Form

use functions\Form; //doesn't seem to work

However, you don't have a namespace named \functions\Form, you have namespace named \Form

namespace Form;

To get this to work, you'd want to

use Form;

or

namespace functions\Form;

Also, in case its not obvious, for reasons historic and frustrating, PHP namespaces do nothing to include/require/autoload PHP files. That's a separate system in PHP.

Community
  • 1
  • 1
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
1

Namespace is a way to avoid the same name function/class error.

It didn't help you do any include.

You should use include in main.php just like: include('form.php'); include('view.php');

Or use spl_autoload_register function, see http://php.net/manual/zh/function.spl-autoload-register.php;

Or use psr0 psr4 with composer, see https://getcomposer.org/

Henry
  • 46
  • 5
0

Turns out I needed to add require statements to load the files form.php and view.php. Also, I had to "use" the namespaces by use Form;.

I had to be wary of any references to classes in my namespaces. For example, throw new Exception() would have to be throw new \Exception() to be in the right namespace.

fibono
  • 773
  • 2
  • 10
  • 23