Parsing a PHP file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
In my PHP script, I'd like to examine another PHP file to get
information on the classes it will define before I include it
(alternatively, getting a list of the classes after I've included it
would be fine too). I realize that I could do some regular expressions
and hope for the best, but my ideal solution has no chance of
misfiring (e.g. capturing something in a comment or string, or missing
something because it's on multiple lines).

My idea at the moment is to pick apart something like Doxygen or GeSHi
and figure out some simple file parsing, but I think that's pretty
involved for my aims here. Does anyone have a suggestion?

Ryan Govostes

Re: Parsing a PHP file

Hm, I just noticed the get_declared_classes() function. I could
probably get a before-and-after list and compare them to figure out
which classes were added. Any other ideas are still welcome.

Re: Parsing a PHP file

On 30 Apr 2007 14:09:42 -0700, in comp.lang.php Ryan  Govostes

Quoted text here. Click to load it

Would any of these be of help
Table of Contents

call_user_method_array - Call a user method given with an array of
parameters [deprecated]  
call_user_method - Call a user method on an specific object
class_exists - Checks if the class has been defined  
get_class_methods - Returns an array of class methods' names  
get_class_vars - Returns an array of default properties of the class  
get_class - Returns the name of the class of an object  
get_declared_classes - Returns an array with the name of the defined
get_declared_interfaces - Returns an array of all declared interfaces  
get_object_vars - Returns an associative array of object properties  
get_parent_class - Retrieves the parent class name for object or class
interface_exists - Checks if the interface has been defined  
is_a - Returns TRUE if the object is of this class or has this class
as one of its parents  
is_subclass_of - Returns TRUE if the object has this class as one of
its parents  
method_exists - Checks if the class method exists  
property_exists - Checks if the object or class has a property  

---------------------------------------------------------------  : Remove your pants to reply

Re: Parsing a PHP file

Ryan Govostes kirjoitti:
Quoted text here. Click to load it

I'm curious, what would you do with this info?

Anyway, as you said, there is get_defined_classes and I think what you  
are trying might be accomplished thusly:

$before = get_defined_classes();


$declared = array_diff(get_defined_classes(), $before);



"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

Re: Parsing a PHP file

Quoted text here. Click to load it

me too. I suspect you really want to learn how to use the autoloader


Site Timeline