Namespace interface
Asked Answered
G

3

7

I have struct:

/VBAL/
/VBAL/Interface/
/VBAL/Interface/Named.php
....
/VBAL/Component.php

Component.php:

namespace JV\VBAL; 
class Component implements \JV\VBAL\Interface\Named {}

Named.php:

namespace JV\VBAL\Interface;
interface Named {}

But I've got parse error:

Parse error: syntax error, unexpected '{', expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR

How do you call the directory "namespace", or place the files?

Granese answered 9/8, 2011 at 10:1 Comment(3)
Are you looking for a Cascade File System method?Ursula
I am looking for a way of placing files in php for the greatest transparency,understanding of the code,the directory interface is more appropriate for the interface files,but it seems so impossible to call the namespace so looking for another name,or a way of placing files, such as how you look at that option: move the file Named. php in the directory /VBAL/Component/ (create directory Component resp. for Component.php),but it deprives the flexibility to use this interface,because it becomes a priority attached to the class Component.Maybe there are ways to accommodate files that I do not knowGranese
check this, might give you some tips : kohanaframework.org/3.2/guide/media/kohana/…Ursula
D
17

Interface is a reserved word in PHP. You can't use it as part of your namespace.

Disarticulate answered 9/8, 2011 at 10:25 Comment(5)
Yeap, I know, but how it resolve, for example, may be generally accepted standards of the name of the directory...Granese
It's not PHP 5.3+ standard for sure. If you want your namespace to reflect your directory structure and vice versa, you can't have namespaces called Interface, Class, Function etc. BTW: You can't have a class called Class or Interface too.Disarticulate
But Interfaces is plural and it's a problem if all folders in the tree are singular names (Model, Form, Exception, ...)Polymerism
Indeed it is. Remember: there are only two hard things in computer science: martinfowler.com/bliki/TwoHardThings.htmlDisarticulate
In my case it was AbstractIndomitability
D
1

As mentioned before Interface is reserved word and can not be used as part of namepsace.

Two good alternatives are:

Base - in which you can store many base classes from which you extend, for example interfaces, abstract classes, traits

or Interfaces, but this one is in my opinion used little less because of it's plular form

Dwight answered 5/3, 2017 at 15:13 Comment(0)
O
0

From php8, reserved keywords such as Interface or Trait can be used as part of the namespace.

<?php

namespace App\Entity\Interface;

interface FooInterface
{

}

https://wiki.php.net/rfc/namespaced_names_as_token

Osrick answered 22/3, 2024 at 14:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.