redbullmarky Posted April 19, 2007 Share Posted April 19, 2007 Hello all from the PHP manual, one user comment states: It wasn't quite clear, but I found out that __get() and __set() only overload attributes that _don't_ exist. my question - is it possible to override this behaviour in any way so that ALL method calls can be filtered, be it using __call or otherwise? I'm trying to trim out all the crap from my framework so it'd be nice to be able to see what methods never get used at all so I can better decide whether to keep them or get rid without breaking sites that currently run on it. Sure, I could write "new" versions where and how I want them and leave the old ones where they are for now, but I've got further plans - I'm working on something that (thanks to a bit of help from prozente here) will actually pull not only the required files from the framework and get them ready for distribution, but will also recreate these files with ONLY the required functions for that site. In testing on a small scale, this is working very well but if I can just filter all method calls, that'd be great. Any thoughts? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/ Share on other sites More sharing options...
redbullmarky Posted April 20, 2007 Author Share Posted April 20, 2007 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-234249 Share on other sites More sharing options...
genericnumber1 Posted April 22, 2007 Share Posted April 22, 2007 I doubt that's possible unless you were to extend php (work), call a logging method at the top of every method call (a headache), or use an interface method to call all other methods (very messy and breaks oh-so-many rules of oop.) I figure you knew all this but just putting my 2 cents in. Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235027 Share on other sites More sharing options...
448191 Posted April 22, 2007 Share Posted April 22, 2007 Ne pas possbile ou le overloading methods.. How about using the backtrace? Write a basic logging class that uses DOM (or simpleXML), ReflectionClass and the backtrace to create an XML file. Whenever a class is used in the execution, it - and methods not in the BT - gets added to the tree. Whenever a method gets used, it's get dumped from the tree. After testing all application features, you should end up with a file listing all unused methods. If you also want unused classes you'll have to traverse the file system. Probably the best point to call this procedure is from the destructor of your frontcontroller or better yet indexcontroller if you use one (which you probably do). Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235081 Share on other sites More sharing options...
redbullmarky Posted April 22, 2007 Author Share Posted April 22, 2007 genericnumber1, yeah i did consider logging each method but some of them are literally only a line or two long as they are, and the resources used in another function call would have probably made it a bit sluggish. I also considered (as it's a long term solution not just a short term clean up thing) renaming all my methods with a prefix (so they wouldnt be recognised at all by apps using them) and running them ALL through a __call method, but again I'd guess this would be quite sluggish. As for extending PHP, I wouldnt know where to start. I've looked also at a few debuggers that seem like they might do the job, but really this kinda does need to be integrated into the framework rather than extensions or other tools. John - yeah I have both an index and front controller, so I guess no issues implementing something like that, just that I find the backtrace quite limited for uses such as this in that it doesnt backtrace that far. I guess I could give it a go on a smaller part and see how it goes...even if it doesnt work, I'll prob still pick up a few useful tricks along the way... Thanks both Mark Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235093 Share on other sites More sharing options...
448191 Posted April 22, 2007 Share Posted April 22, 2007 Yeah, the backtrace is useless for this purpose because it keeps dumping completed codepaths... Sorry, forgot about that. I tried to think of something else, but I came up empty. I'm still thinking about it though.. Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235380 Share on other sites More sharing options...
448191 Posted April 22, 2007 Share Posted April 22, 2007 I can think of one thing that will work, but it's not exactly elegant.. :-\ 1) Create a copy of your framework. 2) Loop though all files in the copy framework, after each method declaration insert a call to the method logger. 3) In the same loop have the method logger populate the XML file with all methods. 3) Test run the copy extensively, have the method logger move (to a new document) or remove used methods. You can use regex to modify the files, this old thread of mine might help: http://www.phpfreaks.com/forums/index.php/topic,114708.0.html Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235441 Share on other sites More sharing options...
redbullmarky Posted April 22, 2007 Author Share Posted April 22, 2007 you mean actually "manipulate" and insert logging code on the fly? (read the php file, add the code, rewrite it). yeah i guess that could work, as i currently have ability to seperate the framework into "production" and "development" versions, so i guess nothing wrong with extending this further... i'll let you know how it goes. cheers! Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235475 Share on other sites More sharing options...
Jenk Posted April 23, 2007 Share Posted April 23, 2007 Use reflection, don't parse files. Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235677 Share on other sites More sharing options...
448191 Posted April 23, 2007 Share Posted April 23, 2007 Mark, I think what Jenk is trying to say is that besides listing methods, you can also get the starting line of method's declaration using Reflection. So if you insert a new line with the call to the logger after the declaration, it should work without regex. Tbh I hadn't thought of that, but it should work. Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235746 Share on other sites More sharing options...
Jenk Posted April 23, 2007 Share Posted April 23, 2007 It'll also save a heck of a lot of time.. <?php $reflect = new ReflectionClass('SomeClass'); echo $reflect->getFileName(); echo $reflect->getParentClass()->getName(); echo $reflect->getParentClass()->getConstructor()->getName(); //etc. ?> Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235930 Share on other sites More sharing options...
redbullmarky Posted April 23, 2007 Author Share Posted April 23, 2007 hmm well Reflection is a bit new to me as a tool as well as the concept, but how would that help me determine which methods are being used through a process? Am I missing something basic? Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-235950 Share on other sites More sharing options...
Jenk Posted April 23, 2007 Share Posted April 23, 2007 Create stubs to tally class/method calls. Quote Link to comment https://forums.phpfreaks.com/topic/47740-overloading/#findComment-236034 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.