Loading classes without "executing" them?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dreamscape
Forum Commoner
Posts: 87
Joined: Wed Jun 08, 2005 10:06 am
Contact:

Loading classes without "executing" them?

Post by dreamscape »

Does anyone know of a way to trick the PHP engine into loading classes into memory without executing them, or at least if there are error, without it effecting the running script?

It may seem like an odd question, but I'm putting together a simple ref doc generator for a project (phpDoc and Doxygen are both good, but neither has exactly what we need), and if I could use Reflection, it would make documenting classes a lot easier. Otherwise I'll have to use tokens, which could get pretty ugly trying to pick out the right pieces.

And the reason I want to be able to load classes without errors stopping the script is so that a class can still be documented even if some dependency is missing. And trying to automatically resolve dependencies would get rather ugly as well.

You used to be able to trick the engine into doing this with php_check_syntax() in the early PHP 5 days, but it was removed in 5.0.5.

Anyone know of a way to trick the engine into loading classes into memory without effecting the running script that still works in PHP 5.1 and 5.2 ?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

I don't think this is possible. You need to include the file with the class in it, and if the dependency does not exist (say the class extends another class that isn't included) you will get a fatal error. I have no idea of how you would get around that. And if you did find a work around, chances are it will end up getting patched.

If this is within one project and you have some sort of framework for including dependencies, maybe you can include your doc generator within that framework so that the dependencies are resolved. I would suggest this approach.

Another idea would be to write a gigantic messy file where you require every single file and all its dependencies that you want to document.

Yet another idea, say you have a file called myClass.php, you would maybe have one called myClass.doc.xml where you define dependencies and other info. Then your documenter can look a the XML file and load in the dependencies.

You can also consider extending phpDoc or forking it for your own use.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
dreamscape
Forum Commoner
Posts: 87
Joined: Wed Jun 08, 2005 10:06 am
Contact:

Post by dreamscape »

Begby wrote:You can also consider extending phpDoc or forking it for your own use.
My main problem with phpDoc is that it is quite bloated, supporting both PHP 4 and PHP 5. Several tags it supports are redundant in PHP 5. The code is also quite messy and somewhat difficult to follow.

I was hoping to use Reflection, but it appears that will be impossible to do so without including the classes, and that leads to all sorts of issues (unresolved dependencies, naming collisions, etc).

I'll just keep on using tokens I guess. The last hurdle was method parameters, and I didn't really want to parse those using tokens, but I sat down for about an hour earlier and worked it out. It's not the prettiest code (working with tokens never is), but it seems to be working.
User avatar
dreamscape
Forum Commoner
Posts: 87
Joined: Wed Jun 08, 2005 10:06 am
Contact:

Post by dreamscape »

volka wrote:Is the runkit extension availabe?
http://de2.php.net/manual/en/function.r ... t-file.php
Does that actually load the class into memory though? I'm not actually interested in checking the file's syntax... I could care less about that. I was interested in the side-effect of php_check_syntax() where it would load classes into memory without killing the script if there was a fatal error, and you could then reflect those classes in the file you checked.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
dreamscape
Forum Commoner
Posts: 87
Joined: Wed Jun 08, 2005 10:06 am
Contact:

Post by dreamscape »

I'm aware of the runkit extension. Some of pre-cursors to the runkit extension, the aggregate_* functions, existed in the PHP 4 core, and I for one was saddened to see them leave the core with PHP 5.

runkit is fantastic in theory; however, at this point in time, I'm not particularly interested in writing any code that relies on PECL.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You might want to look at command line options like -R or --rc. Perhaps you could get information even if there are errors.
(#10850)
Post Reply