I'm not a PHP expert, so I asked my friends who works as PHP Developer a question
"What are they usually need for developing system? What is the best framework did they
use?" So 2 frameworks comes, Eocene and Mojavi.
I'm trying to learn about Mojavi, and I have to learn so many things to start my
project. Oh my god. So I stopped and think "Why don't I learn the concepts, and create
the framework by myself, so I don't have to waste time to learn features and
regret if the framework didn't fit to me".
So I started my simple framework using OO on PHP.
The problem is "how can I handle with unexists class on code time, but exist on
run time?" Google comes to help and I found "reflection".
So there is reflection API on PHP as well.
After that I learn reflection on Java.
Here is an example of how to use reflection API.
package uudashr.reflection;
public class Example {
public void hello() {
System.out.println("This is hello method");
}
public String toString() {
return "This is example instance [from toString method]";
}
}
How to handle this simple class using reflection API? watch this.
package uudashr.reflection;
import java.lang.reflect.Method;
public class GettingClass {
public static void main(String[] args) {
testCreateInstance();
testExecutingMethod();
}
public static void testCreateInstance() {
try {
Class exampleClass = Class.forName("uudashr.reflection.Example");
Object obj = exampleClass.newInstance();
System.out.println(obj.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testExecutingMethod() {
try {
Class exampleClass = Class.forName("uudashr.reflection.Example");
Object obj = exampleClass.newInstance();
Method helloMethod = exampleClass.getMethod("hello", null);
helloMethod.invoke(obj, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment