News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Add custom functions to SMF

Started by CodeDev, March 03, 2024, 03:34:37 PM

Previous topic - Next topic

CodeDev

Hi!

Is there a best way to include some custom functions in SMF?

I mean, I usually hardcode /index.php with a PHP require() to my custom functions file located at /Sources.
It's works very well, but I was wondering if there was some better and built-in approach to properly do this.

Thank you.

Arantor

That mostly depends on what you're trying to do - various options exist.
Holder of controversial views, all of which my own.

CodeDev

Many, many, many things. In a previous version of SMF, I highly customized it with various functions. Whether it was for ad placement, topic features, or SEO...

Arantor

And almost all of these things are done differently now in SMF 2.1 because we've moved the architecture to discourage file editing.

Basically, there's a series of 'integration hooks' (legacy name, really, should be changed somewhat) where the code gets to and says 'hey, I'm here, anybody want to do anything' - you can modify much of the running state at any given point with this.

There are guides in the wiki on what's available and plenty of examples of modern mods doing this, but it really would be easier with concrete examples... for example all of the things you mention are all pretty massive topics to do properly.
Holder of controversial views, all of which my own.


Kindred

it is accurate for what is listed -- I suspect there may be additional hooks that were never added to the wiki yet, though
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Arantor

Well, accurate is... not entirely accurately. When I originally wrote that list, hooks couldn't stack multiple per hook, nor could they handle class static methods nor load files. So the hook docs still say "accepts one function name" except that's not really true - they should accept callables (I suspect they wouldn't accept closures, though as a non-persistent call they *should* just there's little reason why you *would*), and you can pass a file to load first, which I'm not sure if that's documented.

I also doubt it's documented that you can call and get an object instance back but given the lack of control on construction, it's also questionable what that gets you. But you can.

Ultimately we're back to "what are you trying to do" and super-broad one line descriptions don't help because guaranteed what one person means by these, others won't, plus several of the listed items already have existing mods that are perfectly functional.
Holder of controversial views, all of which my own.


Kindred

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Tyrsson

I would add that if it allows including a file, then you can have a closure as a return ;) So, in the event of accepting a file, one would need to accept a closure as well.
PM at your own risk, some I answer, if they are interesting, some I ignore.

Arantor

Hooks can't accept closures if they're saved to the database because you can't serialise a closure to persist it.
Holder of controversial views, all of which my own.

Tyrsson

Quote from: Arantor on March 15, 2024, 03:48:58 AMHooks can't accept closures if they're saved to the database because you can't serialise a closure to persist it.
Who said anything about serializing a closure?
PM at your own risk, some I answer, if they are interesting, some I ignore.

Arantor

You did. That's literally not how SMF hooks work. They can accept a file to load and they can accept a named function to call (either a procedural one or a static class method) but you cannot use a closure with them.
Holder of controversial views, all of which my own.

Tyrsson

The closure would be in the file.
PM at your own risk, some I answer, if they are interesting, some I ignore.

Arantor

SMF hooks don't work that way. They specify a named function to call, with an optional file to load after.

In 2.0, 2.1 and 3.0 the integration hooks start with a string which is the function to call. 2.0 can't even load a file there but 2.1 and 3.0 can, and in all cases this data is coming out of the SMF settings table. Where you can't put a closure to reference.

3.0 straight up won't let you, the IntegrationHook class only accepts strings on the add method, 2.1 doesn't type hint so you could in theory pass a closure there in the add function, but it'll fail trying to be called by call_integration_hook which uses strpos on the function name, which isn't going to work on a closure.

There is no universe that you can use a closure with SMF's integration hooks as the hooked function itself, it just doesn't work because the code simply won't let it. It was never, ever designed to be used with closures.

I was wrong originally when I said they should, as I hadn't checked. Now I have.
Holder of controversial views, all of which my own.

Tyrsson

Quote from: Arantor on May 21, 2024, 09:35:25 AMThere is no universe that you can use a closure with SMF's integration hooks as the hooked function itself
I was not saying use it as the hooked function itself.
 
PM at your own risk, some I answer, if they are interesting, some I ignore.

Arantor

You don't have any choice in the matter.

You can't use the integration hooks to load a file on its own. You can only use the integration hooks to run a named function, and optionally load a file in the process.

Yes, I suppose the named explicit function could return a closure in the file, but it also could *literally just do the work you need it to do because that's why it was called*.

Whatever you think you're trying to argue, the original point you made: use an integration hook to return a closure, and this is a fundamental misunderstanding of how the hooks work and how they have always worked.
Holder of controversial views, all of which my own.

Tyrsson

I'm not trying to argue any point. My original post was worded badly. That's true.

And yes, I understand all the points you are making. I'm not trying to disprove anything you have posted.

My only point was that if the hook can require/include a file then a closure can be executed. Which is 100% correct. Just because that statement is correct doesn't mean you are wrong. You are correct.

Let me say that again just so we are clear.

You are correct.
PM at your own risk, some I answer, if they are interesting, some I ignore.

Arantor

Holder of controversial views, all of which my own.

Advertisement: