Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
The Old Man Posted March 17, 2019 Posted March 17, 2019 Hi, I'm making a plug-in with a code hook in Dev mode on my local Xampp. I read that you shouldn't replicate the source code but the Development documentation doesn't actually go on to provide any explanation on how to otherwise insert our code into the existing functions within a method. If you have several existing IPS functions mixed with chunks of PHP within a method, within a class, inside a system/file/file.php, and you want to extend them or insert additional code, what is the recommended way to do it, please? I may not be describing it correctly so I'm picturing this as an example of what I want to hook into... class xxx extends xxxxxxxx ... method 1 function a { various PHP used in function a } function b { some more PHP used within function b } method 2 function c { Quite a bit of PHP used within c } function d { some PHP used within d } function e { some more PHP used within e } method 3 function f { some more PHP used within f } function g { some more PHP used within function g } etc... I'm thinking that if I want to insert my code using a code hook within function d, I can't just insert it where I want without either A) replicating at least some of the existing code by copying function d into my hook then editing it to add my code or B) maybe even replicating all of method 2 in its entirety including all 3 of its functions (c,d,e) in my new hook, as it can't be split up/avoided? Is that correct? Secondly, if it were an app I was making instead of a plug-in, does it work the same way still using a code hook and copying the function or method into my hook? Many thanks.
Aiwa Posted March 17, 2019 Posted March 17, 2019 Apps vs plug-ins makes no difference in code hook functionality.
The Old Man Posted March 18, 2019 Author Posted March 18, 2019 Thank you. I think I understand it. So you don't make a copy of the original function/s you want to extend, just reference the parent function using parent::functionname? Does it still run or end up running twice, or does the code hook overrule the parent? 3 hours ago, Aiwa said: Apps vs plug-ins makes no difference in code hook functionality. Great, thanks for the help.
Aiwa Posted March 18, 2019 Posted March 18, 2019 49 minutes ago, The Old Man said: So you don't make a copy of the original function/s you want to extend, just reference the parent function using parent::functionname? Yes You should brush up on inheritance. If you don't call the parent you break the chain, execution stops with what you tell it to do. So long as you return the parent, via parent::functionname(), you maintain geneology and execution continues. You can't insert little lines of code in the middle of a method. If that's what you're after you need to find another hook point.
The Old Man Posted March 18, 2019 Author Posted March 18, 2019 Thanks for the tips Aiwa, much appreciated. 🙂 I'm more used to procedural PHP and database queries as you can tell, haven't done much object-oriented PHP. Time to dig out the tutorials! http://php.net/manual/en/language.oop5.inheritance.php
Recommended Posts
Archived
This topic is now archived and is closed to further replies.