Getting class object information from file.
Getting class object information from file.
am 16.01.2008 22:29:41 von elmosik
Is there any method to get this informations from php file?
1. Class methods
2. Check whether class in file is extended by 'XXX' or not (class
MyClass extends XXX ...)
- Class has same name as files e.g. file named 'myclass.php' has class
named 'myclass' etc...
I'm trying to do that tasks from list in that way:
-----------------------------------------------------
$class = 'myclass';
ob_start();
include($class.'.php');
// Creating object instance
$oClass = new $class;
// Check if $class extends XXX
if ( is_subclass_of( $oClass, 'XXX' ) )
{
// Class extends XXX
}
ob_end_clean();
--------------------------------------------------------
Yea, it's simple and working, but what happen when in __construct()
method in that class we try to run function from parent class:
------------------------------ myclass.php ---------------
class MyClass extends XXX
{
public __construct()
{
$this->method_from_XXX_class();
}
}
------------------------------------------------------------ ------
.... and what now? we've got a nice error while including myclass.php
and creating instance (new MyClass): "Call to a member function
method_from_XXX_class() on a non-object."
It's also dangerous because of potentially malicious code in included
file, but how can I get in other way? The only one solution that I
have in my mind is using regular expressions to extract interesting
data or ...?
Any ideas?
Thanks in advance for help and sorry for my English.
Re: Getting class object information from file.
am 17.01.2008 05:24:22 von Jerry Stuckle
elmosik@gmail.com wrote:
> Is there any method to get this informations from php file?
>
> 1. Class methods
> 2. Check whether class in file is extended by 'XXX' or not (class
> MyClass extends XXX ...)
>
> - Class has same name as files e.g. file named 'myclass.php' has class
> named 'myclass' etc...
>
> I'm trying to do that tasks from list in that way:
>
> -----------------------------------------------------
> $class = 'myclass';
>
> ob_start();
>
> include($class.'.php');
>
> // Creating object instance
> $oClass = new $class;
>
> // Check if $class extends XXX
> if ( is_subclass_of( $oClass, 'XXX' ) )
> {
> // Class extends XXX
> }
>
> ob_end_clean();
> --------------------------------------------------------
>
> Yea, it's simple and working, but what happen when in __construct()
> method in that class we try to run function from parent class:
>
> ------------------------------ myclass.php ---------------
> class MyClass extends XXX
> {
> public __construct()
> {
> $this->method_from_XXX_class();
> }
> }
> ------------------------------------------------------------ ------
>
> ... and what now? we've got a nice error while including myclass.php
> and creating instance (new MyClass): "Call to a member function
> method_from_XXX_class() on a non-object."
>
You should only be calling the constructor of the parent class in your
constructor.
> It's also dangerous because of potentially malicious code in included
> file, but how can I get in other way? The only one solution that I
> have in my mind is using regular expressions to extract interesting
> data or ...?
>
You can have malicious code in any file. It's not restricted to class
files.
> Any ideas?
>
> Thanks in advance for help and sorry for my English.
>
No problem at all with your English :-)
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Getting class object information from file.
am 17.01.2008 12:19:40 von elmosik
On 17 Sty, 05:24, Jerry Stuckle wrote:
> You should only be calling the constructor of the parent class in your
> constructor.
Yes it's normal when you create new instance of included class,
constructor is called automatically ('new Class()'). Remember that in
constructor I can have also 'include/require' directives and it's the
main problem of that.
I had it coded in the second way that I described before (regexp) and
it work correctly, the main problem was how to choose valid regular
expressions to classes, functions, comments etc.
It's basicly to do in 3 steps:
1. Remove comments from file, they can consists faked classes and
functions e.g.
- /* class MyClass extends XXX */
- # public function test()
- // class MyClass extends XXX
2. Match with regexp class context:
- class .... { ..... }
- get class context and match functions pattern
3. Remove sweepings and here you go
My patterns (PHP preg_), they work for me now but I don't know in 100%
that they are valid - I need to test them more precisely.
Comments
------------------------
Multi line comments:
'@/\*.*?\*/@ims'
Hashed lines:
'@^[\s|\t]+#.*?$@ims'
Double slashed lines:
'@^[\s|\t]+//.*?$@ims'
Classes, functions
-------------------------
From one 'class' directive to other or to '?>':
'@^[\s|\t]+class[\s|\t]+([A-Z0-9_]+)([^{]+)?.*?(?:class|\?\> )@ism'
Probably not working for code like that (divided into few
directives):
------------ sample code
class MyClass extends XXX
{
}
?>
class MySecondClass...
?>
------------------------
Check if class extends XXX:
'@extends[\s|\t]+XXX@ism'
Look only for public methods:
'@^[\s|\t]+(?:function|public[\s\t]+function)[\s|\t]+([A-Z0- 9_]+)@ims'
If you saw dramatic mistakes in my patterns say that :) I correct
them.
--
Thanks and see you
Re: Getting class object information from file.
am 17.01.2008 13:29:01 von Toby A Inkster
elmosik wrote:
> Multi line comments:
> '@/\*.*?\*/@ims'
>
> Hashed lines:
> '@^[\s|\t]+#.*?$@ims'
>
> Double slashed lines:
> '@^[\s|\t]+//.*?$@ims'
Regular expressions simply do not cut it. Consider the following comments
which will not be caught by your regular expressions:
define('SOME_CONFIG_OPTION', TRUE); # Here is a comment
define('SOME_OTHER_OPTION', TRUE); // Here is a comment
But what about the following code which contains no comments, which will
be treated as comments by your regexs:
$comment = "/* Lala */
// Lala
## Lala
No comments here!";
print $comment;
Regular expressions are no good for any non-trivial parsing task. You need
a stateful parser.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 17 days, 23:36.]
Gnocchi all'Amatriciana al Forno
http://tobyinkster.co.uk/blog/2008/01/15/gnocchi-allamatrici ana/
Re: Getting class object information from file.
am 17.01.2008 14:16:42 von elmosik
On 17 Sty, 13:29, Toby A Inkster
wrote:
> Regular expressions simply do not cut it. Consider the following comments
> which will not be caught by your regular expressions:
>
> define('SOME_CONFIG_OPTION', TRUE); # Here is a comment
> define('SOME_OTHER_OPTION', TRUE); // Here is a comment
Patterns used with preg_replace works fine. preg_replace($pattern,
''); - from pattern to empty string and I have clean code.
> But what about the following code which contains no comments, which will
> be treated as comments by your regexs:
>
> $comment = "/* Lala */
> // Lala
> ## Lala
> No comments here!";
> print $comment;
It's not problem for me because I don't need code from functions/
classes like variables etc. I need only to extract functions/classes
names. So example above isn't a problem, but I understand what you
wrote, but remember that it's not a php file parser, I'm not parsing
code from methods...
> Regular expressions are no good for any non-trivial parsing task. You need
> a stateful parser.
Any examples of stateful parser?
--
Thanks
Re: Getting class object information from file.
am 17.01.2008 14:27:35 von luiheidsgoeroe
On Wed, 16 Jan 2008 22:29:41 +0100, wrote:
> Is there any method to get this informations from php file?
>
> 1. Class methods
> 2. Check whether class in file is extended by 'XXX' or not (class
> MyClass extends XXX ...)
>
> - Class has same name as files e.g. file named 'myclass.php' has class
> named 'myclass' etc...
While getting the class name is something you can solve yourself, for
everything following that:
http://php.net/reflection
--
Rik Wasmus
Re: Getting class object information from file.
am 17.01.2008 14:32:54 von elmosik
On 17 Sty, 13:29, Toby A Inkster
wrote:
> Regular expressions simply do not cut it. Consider the following comments
> which will not be caught by your regular expressions:
>
> define('SOME_CONFIG_OPTION', TRUE); # Here is a comment
> define('SOME_OTHER_OPTION', TRUE); // Here is a comment
>
define('SOME_CONFIG_OPTION', TRUE); # class foo() { function bar(){} }
class MyClass extends XXX
{
/* some comment */ function foo() {}
}
.... works correctly in above cases.
Comment after define() is ignored so function bar() is not listed in
results same like with '//'.
Comment before foo() method in class is listed in results.
Everything works fine for me, remember that isn't a universal parser I
need it only working for my specified schema.
--
Thanks
Re: Getting class object information from file.
am 17.01.2008 14:41:13 von elmosik
On 17 Sty, 14:27, "Rik Wasmus" wrote:
> While getting the class name is something you can solve yourself, for
> everything following that:http://php.net/reflection
What can I say, I didn't know about this feature. Rik you're great!
Thanks in advance it was that I needed :)
--
Thanks
Re: Getting class object information from file.
am 17.01.2008 14:49:42 von Jerry Stuckle
elmosik@gmail.com wrote:
> On 17 Sty, 05:24, Jerry Stuckle wrote:
>
>> You should only be calling the constructor of the parent class in your
>> constructor.
>
> Yes it's normal when you create new instance of included class,
> constructor is called automatically ('new Class()'). Remember that in
> constructor I can have also 'include/require' directives and it's the
> main problem of that.
>
Normally you do not include/require something within a constructor. And
you do have to call the parent class' constructor manually if you have
one in the derived class.
> I had it coded in the second way that I described before (regexp) and
> it work correctly, the main problem was how to choose valid regular
> expressions to classes, functions, comments etc.
>
> It's basicly to do in 3 steps:
>
> 1. Remove comments from file, they can consists faked classes and
> functions e.g.
> - /* class MyClass extends XXX */
> - # public function test()
> - // class MyClass extends XXX
>
> 2. Match with regexp class context:
> - class .... { ..... }
> - get class context and match functions pattern
>
> 3. Remove sweepings and here you go
>
> My patterns (PHP preg_), they work for me now but I don't know in 100%
> that they are valid - I need to test them more precisely.
>
> Comments
> ------------------------
> Multi line comments:
> '@/\*.*?\*/@ims'
>
> Hashed lines:
> '@^[\s|\t]+#.*?$@ims'
>
> Double slashed lines:
> '@^[\s|\t]+//.*?$@ims'
>
>
> Classes, functions
> -------------------------
> From one 'class' directive to other or to '?>':
> '@^[\s|\t]+class[\s|\t]+([A-Z0-9_]+)([^{]+)?.*?(?:class|\?\> )@ism'
>
> Probably not working for code like that (divided into few
> directives):
>
> ------------ sample code
> class MyClass extends XXX
> {
> }
> ?>
>
>
> class MySecondClass...
> ?>
> ------------------------
>
> Check if class extends XXX:
> '@extends[\s|\t]+XXX@ism'
>
> Look only for public methods:
> '@^[\s|\t]+(?:function|public[\s\t]+function)[\s|\t]+([A-Z0- 9_]+)@ims'
>
> If you saw dramatic mistakes in my patterns say that :) I correct
> them.
>
> --
> Thanks and see you
>
I'm not an expert on regexs, so I won't comment on them specifically.
But unless you're going to build a complete PHP parser, someone will
find a way around almost anything you do.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Getting class object information from file.
am 17.01.2008 17:04:03 von Toby A Inkster
elmosik wrote:
> Toby A Inkster wrote:
>
>> Regular expressions are no good for any non-trivial parsing task. You
>> need a stateful parser.
>
> Any examples of stateful parser?
I've written a small stateful parser that parses a simple programming
language (called "TrivialEncoderScript") which is used to script the
encryption of some input test to some output text.
The idea of TrivialEncoderScript is that you use it to specify a chain of
encryption techniqies: for example the input text might be encoded using
Triple-DES with a passphrase "hello", then Base64-encoded to make it ASCII-
safe, and then Rot13-encoded just for fun. In TrivialEncoderScript, you
could express that process like this:
tripledes "hello";
base64;
rot13;
To decode the message, you'd use TrivialEncoder in decryption mode, with
the same script (you don't need to reverse the order of the operations --
the decoder knows to do this by itself).
As it doesn't have any control structures, this might seem like quite an
easy scripting language, and something that can be handled by regular
expressions. A level of complexity is added though because of the "multi"
encoding technique, which allows the encryption to branch in multiple
directions. Then you might end up with a script like this:
multi
(tripledes "la;la")
(blowfish "la)la"; memfrob)
(multi
(rot47; rijndael512 "la(la")
(cast256)
)
(rc2 "la\"la")
;
hex;
morse;
The output of such a script would just look like morse code, but in
practise to decode it without knowing the script that produced it would
require cracking several of the world's best encryption algorithms!
Anyway, TrivialEncoder/0.2 is here:
http://tobyinkster.co.uk/blog/2007/08/19/trivial-encoder/
The parser for TEScript is the class TE_Parser in file TE_Machine.class.php
(Actually the above complicated TEScript does not work in the current
version. Nothing wrong with the parser -- just a bug in the implementation
of "multi".)
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 18 days, 2:39.]
Gnocchi all'Amatriciana al Forno
http://tobyinkster.co.uk/blog/2008/01/15/gnocchi-allamatrici ana/