Jump to content

PHP typing questions from a Java programmer


rubenski

Recommended Posts

Hello everyone,

 

I am trying the PHP reflection API and I ran into two questions.

 

I have the following code:

 

$modelReflector = new ReflectionClass('Course');
$methods = $modelReflector->getMethods();

foreach($methods as $key => $value){
   echo "method: " . $value->getName() . "<br>";	
}

 

This code prints the methods in the Course class. My questions are

 

1. Why does getMethods() take no value or a 'long' value? Can't I pass in a regex somehow to select only setter methods?

 

2. I noticed that my editor doesn't know the type (and associated fields and methods) of the $value object in the foreach loop. I know it's an object, I found out that there is a getName() method because php.net says so, but my editor doesn't know it. Is there some way to define the type for an array? Or can I cast value to the appropriate type inside the foreach loop?

 

Thanks!

 

From the comments in the PHP online documentation

//The long $filter values!!!

ReflectionMethod::IS_STATIC;

ReflectionMethod::IS_PUBLIC;

ReflectionMethod::IS_PROTECTED;

ReflectionMethod::IS_PRIVATE;

ReflectionMethod::IS_ABSTRACT;

ReflectionMethod::IS_FINAL;

 

//Use them like this
$R = new ReflectionClass("MyClass");
//print all public methods
foreach ($R->getMethods(ReflectionMethod::IS_PUBLIC) as $m)
    echo $m->__toString();

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.