Class introspection

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
kasuals
Forum Commoner
Posts: 28
Joined: Thu Aug 11, 2011 12:59 pm

Class introspection

Post by kasuals »

Feel free to move this if it's not the correct forum, however I don't need help with any code just help on how I should tackle a task.

I was handed the grunt task of creating a knowledge base that will take in PHP code, read the classes and create a KB of methods, properties, etc.

My first thought was to read in each file, and then filter out the classes w/regex and place them in the DB for future use.

Then I thought, man that's a lot of regex... so I'm debating on loading each file w/include during the indexing and introspect each class from get_declared_classes. The latter seems like it would be less error prone.

Any thoughts? I have no one to bounce ideas off today, so I figured maybe I could pick your brains. :D
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Class introspection

Post by Jonah Bron »

What is the purpose of this project? You might consider using some of the code from documentation generators like DocBlox or PHPDocumentor (DocBlox is more recent).
User avatar
kasuals
Forum Commoner
Posts: 28
Joined: Thu Aug 11, 2011 12:59 pm

Re: Class introspection

Post by kasuals »

I've looked at DocBlox, and it's far more than I need for what I'm trying to do. I just need a simple KB for storing classes/interfaces.

I think I'll just do the introspection. Thanks for the suggestions though!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Class introspection

Post by Jonah Bron »

kasuals wrote:I've looked at DocBlox, and it's far more than I need for what I'm trying to do. I just need a simple KB for storing classes/interfaces.
I didn't suggest that you use the entirity of DocBlox, only that you use the part of it that extracts information about classes. What is "KB"?
kasuals wrote:I think I'll just do the introspection.
What do you mean "introspection"? Are you talking about reflection? That might be a good idea, I didn't think of that. You might run into some problems with name-collision though. The best way to go about that I think is with runkit_import(). That will import functions and classes from a file (just like include()), while ignoring any code that's not in a class/function, and overwriting any existing class/functions.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Class introspection

Post by pickle »

KB = Knowledge Base
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
kasuals
Forum Commoner
Posts: 28
Joined: Thu Aug 11, 2011 12:59 pm

Re: Class introspection

Post by kasuals »

Yeah, I was going to use reflection to gut each class. Might as well take advantage of those functions.

Thanks for the tip on runkit_import(), that actually solved the issue of name-collision which I was just going over with a buddy. Appreciate the tip on that.

I know everyone complains about "re-inventing the wheel", but without deadlines like most coders have I have the luxury of trial and error. I gain more from the creation process than I do researching others code. I figured there was source out there that would do it for me, and other people's code may enlighten me but I don't absorb concepts well unless I've done it myself from start to finish. :crazy:

Thanks for the help guys. I'm sure you'll see me griping in the Code forum. :wink:
Post Reply