ajile
ajile

Reputation: 686

Namespace interface

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?

Upvotes: 7

Views: 5626

Answers (3)

Baishu
Baishu

Reputation: 1611

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

Upvotes: 0

LPodolski
LPodolski

Reputation: 2878

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

Upvotes: 1

Mchl
Mchl

Reputation: 62395

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

Upvotes: 17

Related Questions