Re: [PHP] Re: question regarding Reflection

2005-11-03 Thread Jochem Maas

4 things you need to look into:

1. is_file() and is_readable()
2. $argv (contains what was passed along with the cmdline)
3. __autoload()
4. include_path (.ini setting)

I trust that you capable of STFW and RTFM well enough to
take those 4 and make a start at creating something that resembles
'class loading functionality'?



Manish Marathe wrote:

On 11/1/05, Curt Zirzow <[EMAIL PROTECTED]> wrote:


On Tue, 01 Nov 2005 14:31:12 -0800, Manish Marathe wrote:



...
My question is to use the Reflection API, I need to import the original
class for which I am generating tests. For example to generate a tests


for


class Company which is in Company.php:

include_once('company.php');

$c = new ReflectionClass('Company');

If I have to generate test for many classes at once, it would be foolish
to have them included in my file on the runtime. Am I understanding it
correctly ?


Yes you are understanding correctly. You can avoid this issue by using
the pcntl[1] tools available in php (assuming this is a *nix environment.)
A simple usage would be something like:






The parent process would just iterate through all the class files


you need to include and fork a child. Then you can include the file in
the child process run your unit test and then exit.

I use this method for any sort of long lasting script that causes a lot of
memory or resource usage.




Wow, this certainly helps when we actually run the unit tests and I didn't
think of this earlier so my next question has been already answered by you I
guess if I understand you.

My current problem is this: Below is some chunk of the scrip I am writing to
generate test code.

include_once "Company.php";

class TestGenerator {

public function TestGenerator() {

}

public function catchReflection() {

$classInfo = new ReflectionClass('Company');
// Below here I will be getting all the information about the class
"Company" and then I will be generating a test class CompanyTest which
resides in the file CompanyTest.php.
}
}

So in this script of mine I have included the Company.php above because the
statement $classInfo = new ReflectionClass('Company'); uses it. Now this is
just an example I have taken to see how my generator works. The user would
use the TestGenerator something like this:

php TestGenerator.php myClass

Now is there a way that in my script I can let PHP look for a file
myClass.php or for that matter any php file that has myClass so that I don't
have to include it, as it is totally upto the user which file its gonna pass
to the TestGenerator.php script to generate the test code and I cannot
statically include that in my file. I believe this time I have stated my
problem clearer, earlier I did not, I apologize.

Thank You

 Manish Marathe
SpikeSource, Inc.
http://developer.spikesource.com



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: question regarding Reflection

2005-11-02 Thread Curt Zirzow
On Wed, 02 Nov 2005 10:29:25 -0800, Manish Marathe wrote:
> ...
> My current problem is this: Below is some chunk of the scrip I am writing
> to generate test code.
> 
> include_once "Company.php";
> 
> class TestGenerator {
> 
> public function TestGenerator() {
> 
> 
> }
> public function catchReflection() {
> ...
>
> So in this script of mine I have included the Company.php above because
> the statement $classInfo = new ReflectionClass('Company'); uses it. Now
> this is just an example I have taken to see how my generator works. The
> user would use the TestGenerator something like this:
> 
> php TestGenerator.php myClass
> 
> Now is there a way that in my script I can let PHP look for a file
> myClass.php or for that matter any php file that has myClass so that I
> don't have to include it, as it is totally upto the user which file its
> gonna pass to the TestGenerator.php script to generate the test code and I

I believe it was mentioned in the other thread but just have in your
script:
  ini_set('include_path', '.');

And you script would do something like:
  require($argv[1] . '.php'); // assuming you validated argv[1]

And then you can safely do something like:
  try {
$t = new ReflectionClass($argv[1]);
  } 
  catch(Exception $e) {
echo "Failed trying to create {$argv[1]}\n"; 
echo $e;
exit();
  }

And the user runs the script with:
  php TestGenerator.php MyClass

As long as the naming convention happens to be that the class definition
must be the same as the filename (including case and without the .php of
course) this will work. Of course you could always add another argument to
your script so they can override the default include file used:

  php TestGenerator.php MyClass class.myclass.php

Where class.myclass.php is the file that defines MyClass.

HTH,

Curt.
-- 
http://news.zirzow.dyndns.org/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: question regarding Reflection

2005-11-02 Thread Manish Marathe
On 11/1/05, Curt Zirzow <[EMAIL PROTECTED]> wrote:
>
> On Tue, 01 Nov 2005 14:31:12 -0800, Manish Marathe wrote:
>
> > ...
> > My question is to use the Reflection API, I need to import the original
> > class for which I am generating tests. For example to generate a tests
> for
> > class Company which is in Company.php:
> >
> > include_once('company.php');
> >
> > $c = new ReflectionClass('Company');
> >
> > If I have to generate test for many classes at once, it would be foolish
> > to have them included in my file on the runtime. Am I understanding it
> > correctly ?
>
> Yes you are understanding correctly. You can avoid this issue by using
> the pcntl[1] tools available in php (assuming this is a *nix environment.)
> A simple usage would be something like:
>
> 
> $pid = pcntl_fork();
> if ($pid == -1) {
>
> die('could not fork');
>
> } else if ($pid) {
>
> // we are the parent
> pcntl_wait($status); //Protect against Zombie children
>
> echo 'Class is defined: ';
> var_dump(class_exists('foobar', false));
>
> } else {
>
> // we are the child
> // create a class to see if the parent sees it
> class foobar { }
>
> // do your Unit testing..
>
> }



The parent process would just iterate through all the class files
> you need to include and fork a child. Then you can include the file in
> the child process run your unit test and then exit.
>
> I use this method for any sort of long lasting script that causes a lot of
> memory or resource usage.


Wow, this certainly helps when we actually run the unit tests and I didn't
think of this earlier so my next question has been already answered by you I
guess if I understand you.

My current problem is this: Below is some chunk of the scrip I am writing to
generate test code.

include_once "Company.php";

class TestGenerator {

public function TestGenerator() {

}

public function catchReflection() {

$classInfo = new ReflectionClass('Company');
// Below here I will be getting all the information about the class
"Company" and then I will be generating a test class CompanyTest which
resides in the file CompanyTest.php.
}
}

So in this script of mine I have included the Company.php above because the
statement $classInfo = new ReflectionClass('Company'); uses it. Now this is
just an example I have taken to see how my generator works. The user would
use the TestGenerator something like this:

php TestGenerator.php myClass

Now is there a way that in my script I can let PHP look for a file
myClass.php or for that matter any php file that has myClass so that I don't
have to include it, as it is totally upto the user which file its gonna pass
to the TestGenerator.php script to generate the test code and I cannot
statically include that in my file. I believe this time I have stated my
problem clearer, earlier I did not, I apologize.

Thank You

 Manish Marathe
SpikeSource, Inc.
http://developer.spikesource.com


[PHP] Re: question regarding Reflection

2005-11-01 Thread Curt Zirzow
On Tue, 01 Nov 2005 14:31:12 -0800, Manish Marathe wrote:

> ...
> My question is to use the Reflection API, I need to import the original
> class for which I am generating tests. For example to generate a tests for
> class Company which is in Company.php:
> 
> include_once('company.php');
> 
> $c = new ReflectionClass('Company');
> 
> If I have to generate test for many classes at once, it would be foolish
> to have them included in my file on the runtime. Am I understanding it
> correctly ?

Yes you are understanding correctly.  You can avoid this issue by using
the pcntl[1] tools available in php (assuming this is a *nix environment.)
A simple usage would be something like:

http://php.net/pcntl
 
HTH,

Curt.
-- 
http://news.zirzow.dyndns.org/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php