473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Getting class object information from file.

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.
Jan 16 '08 #1
8 1786
el*****@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.
js*******@attglobal.net
==================

Jan 17 '08 #2
On 17 Sty, 05:24, Jerry Stuckle <jstuck...@attglobal.netwrote:
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 <?php ?>
directives):

------------ sample code
class MyClass extends XXX
{
}
?>

<?php
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
Jan 17 '08 #3
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/0...llamatriciana/
Jan 17 '08 #4
On 17 Sty, 13:29, Toby A Inkster <usenet200...@tobyinkster.co.uk>
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
Jan 17 '08 #5
On Wed, 16 Jan 2008 22:29:41 +0100, <el*****@gmail.comwrote:
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
Jan 17 '08 #6
On 17 Sty, 13:29, Toby A Inkster <usenet200...@tobyinkster.co.uk>
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
Jan 17 '08 #7
On 17 Sty, 14:27, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
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
Jan 17 '08 #8
el*****@gmail.com wrote:
On 17 Sty, 05:24, Jerry Stuckle <jstuck...@attglobal.netwrote:
>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 <?php ?>
directives):

------------ sample code
class MyClass extends XXX
{
}
?>

<?php
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.
js*******@attglobal.net
==================

Jan 17 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jacqueline Snook | last post by:
Hi, I am working on a website that uses a SVG diagram with multiple polygons (to represent rooms in a hotel). Each polygon has a class to represent whether the room is single or double. In a...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
4
by: Larry Tate | last post by:
I am wanting to get those cool html error pages that ms produces when I hit an error in asp.net. For instance, when I get a compilation error I get an html error page that shows me the ...
3
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are...
8
by: Brent White | last post by:
If you need more information, I'd be glad to give it, but I am developing an ASPX page so that the user can select multiple values for a specific field, then pass them on (using Crystal Reports 10...
1
by: Brian Henry | last post by:
I need to mark assemblies with a unique id (GUID in this case) and retrieve it when the assembly is loaded into my application (a plug-in loader)... the plug-in assembly all have a class in it...
5
by: mike | last post by:
Hi, I have been playing with VB.NET/C# for getting some general properties of a fileinfo object. However, FileInfo object does not seem to expose some of the basic properties like TYPE that used...
3
by: Eroc | last post by:
I'm new to XML files so I'm kinda lost here. I found some example code on reading an XML file. My objective is simple. Read the whole XML file into memory. Here is part of my code: Private...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.