Asked  7 Months ago    Answers:  5   Viewed   33 times

Is there any reflection/introspection/magic in PHP that will let you find the PHP file where a particular class (or function) was defined?

In other words, I have the name of a PHP class, or an instantiated object. I want to pass this to something (function, Reflection class, etc.) that would return the file system path where the class was defined.

/path/to/class/definition.php

I realize I could use (get_included_files()) to get a list of all the files that have been included so far and then parse them all manually, but that's a lot of file system access for a single attempt.

I also realize I could write some additional code in our __autoload mechanism that caches this information somewhere. However, modifying the existing __autoload is off limits in the situation I have in mind.

Hearing about extensions that can do this would be interesting, but I'd ultimately like something that can run on a "stock" install.

 Answers

85

Try ReflectionClass

  • ReflectionClass::getFileName — Gets a filename

Example:

class Foo {}
$reflector = new ReflectionClass('Foo');
echo $reflector->getFileName();

This will return false when the filename cannot be found, e.g. on native classes.

Wednesday, March 31, 2021
 
braindamage
answered 7 Months ago
75

Chances are you are importing the file that declares the class more than once. This can be symptomatic of includes/requires getting out of control so you may need to simply your structure.

One alternative approach is to use autoload to load classes to avoid this kind of problem. Another is to only use include_once or require_once. I generally prefer to use require with a simple structure.

Wednesday, March 31, 2021
 
Blur
answered 7 Months ago
66

How are you running the script now? Windows or Linux? Linux is a no-brainer with cron: on a PHP-enabled server simply drop the PHP script somewhere, edit the crontab and away you go!

Ex. for every 2 hours

0 */2 * * * /usr/local/bin/php /path/to/script.php

Edit Re: Mac

launchd is apparently the preferred method to run scheduled tasks but I understand that OS X has cron capabilities as well being a UNIX derivative.

Saturday, May 29, 2021
 
sohum
answered 5 Months ago
46

Use the class ReflectionClass:

On Codepad.org: http://codepad.org/VEi5erFw
<?php

class MyClass
{
  public function func1(){}
  public static function func2(){}
}

$reflection = new ReflectionClass('MyClass');
var_dump( $reflection->getMethods(ReflectionMethod::IS_STATIC) );

This will output all static functions.

Or if you want to determine whether a given function is static you can use the ReflectionMethod class:

On Codepad.org: http://codepad.org/2YXE7NJb

<?php

class MyClass
{
  public function func1(){}
  public static function func2(){}
}

$reflection = new ReflectionClass('MyClass');

$func1 = $reflection->getMethod('func1');
$func2 = $reflection->getMethod('func2');

var_dump($func1->isStatic());
var_dump($func2->isStatic());
Monday, August 16, 2021
 
BenOfTheNorth
answered 2 Months ago
39

How about

  • ReflectionProperty::getValue - Gets the properties value.

In your case:

foreach ($api->getProperties() as $propertie)
{
    print $propertie->getName() . "n";
    print $propertie->getValue($t);
}

On a sidenote, since your object has only public members, you could just as well iterate it directly

foreach ($t as $propertie => $value)
{
    print $propertie . "n";
    print $value;
}

or fetch them with get_object_vars into an array.

Monday, August 16, 2021
 
Sagar
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :