Page 1 of 1

Importing Code From Other Probjects

Posted: Fri Nov 18, 2016 5:28 pm
by Haravikk
So I'm finally getting back round to finishing some extensions (previously mods) that I've been putting off, however I've encountered one thing I'm a little unsure about (likely many more to follow though):

Thing is, one of my mods is ideally placed to benefit from code I've done in the past, which I always namespace using reverse URI notation like so: com\haravikk\foo\bar

However, if I want to add this to an extension do I have no choice but to add the \vendor_name\mod part to the beginning? If this is the case it is not going to be pretty as I was hoping to just copy in these classes without having to change them, as there are rather a lot. I suppose I can use some kind of build script to get around this if I have to (i.e- edit namespaces automatically) but it's not something I really want to do, and I fully expect it to go horribly wrong.

Is there any way in PHP to group classes under a namespace even if that's not what they have in the files themselves? e.g- so I can force the classes to be loaded as: haravikk\myawesomemod\com\haravikk\foo\bar?

Does anyone else use outside code a lot? How are you handling it?

Re: Importing Code From Other Probjects

Posted: Fri Nov 18, 2016 6:48 pm
by david63
Not sure what it is that you are saying but having multiple "use" statements is not a problem.

Re: Importing Code From Other Probjects

Posted: Fri Nov 18, 2016 7:21 pm
by Haravikk
What I mean is; the code I want to add to my extension is all namespaced as \com\haravikk\blah\blah, but this doesn't meet the requirement that extensions classes be in a namespace beginning with haravikk\mycoolextension, so is there a way for me to add this to my imported code without having to change all of the files?

Re: Importing Code From Other Probjects

Posted: Fri Nov 18, 2016 9:33 pm
by david63
An extension can use any namespace you want - just as long as it can access the files. I am guessing that this will be only for your use and will not be being submitted to the CDB so you are free to do whatever you want.

Re: Importing Code From Other Probjects

Posted: Fri Nov 18, 2016 10:05 pm
by kasimi
Note that in case your extension renders templates from within a controller and your controller class doesn't follow namespacing convention you need to manually set your style paths because phpBB will fail to set them up for you: https://github.com/phpbb/phpbb/blob/rel ... #L108-L119

Re: Importing Code From Other Probjects

Posted: Fri Nov 18, 2016 10:19 pm
by Haravikk
david63 wrote:An extension can use any namespace you want - just as long as it can access the files. I am guessing that this will be only for your use and will not be being submitted to the CDB so you are free to do whatever you want.
Why would you guess that? It is not the case; all I'm trying to do is ask if there's an easier way to do this, FFS I don't know why I even bothered now… :?

Re: Importing Code From Other Probjects

Posted: Fri Nov 18, 2016 10:57 pm
by VSE
Code you've done in the past... Sounds like something you would just import into your extension. In which case that part can be namespaced whatever you want. Just the extension itself should be namespaced with your vendor/name to prevent collisions with other extensions..