Results 1 to 3 of 3
I have the following PHP class which is the root of the inheritance tree of every PHP class I write. It uses reflection and magic methods to allow every protected ...
- 03-08-2009 #1
Java equivalent coding to this PHP
I have the following PHP class which is the root of the inheritance tree of every PHP class I write. It uses reflection and magic methods to allow every protected instance variable in it's sub-classes to automagically have a get and set property.
I wrote this purely because I am too lazy to write get and set methods for every damn variable! I am finding that I have the same laziness now I am starting to get into Java.
Is it possible or even advisable to something similar to this?
PHP Code:abstract class Object {
public function __get($strProperty) {
$objReflect = new ReflectionClass($this);
$strProperty = strtoLower($strProperty[0]) . substr($strProperty, 1);
foreach ($objReflect->getDefaultProperties() as $key => $strProp) {
$key = strtoLower($key[0]) . substr($key, 1);
if ($key == $strProperty) {
return $this->{$key};
}
}
}
public function __set($strProperty, $strData) {
$objReflect = new ReflectionClass($this);
$strProperty = strtoLower($strProperty[0]) . substr($strProperty, 1);
foreach ($objReflect->getDefaultProperties() as $key => $strProp) {
$key = strtoLower($key[0]) . substr($key, 1);
if ($key == $strProperty) {
$this->{$key} = $strData;
}
}
}
}
Thanks.If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 03-09-2009 #2Just Joined!
- Join Date
- Feb 2009
- Posts
- 45
What purpose do these universal getters / setters fulfil that isnʼt already provided by declaring these variables public? The whole point of getters and setters is to have encapsulation and abstraction.
Originally Posted by elija
Due to these accessors being universal, thereʼs certainly no encapsulation.
Abstraction is severely impaired, because instead of having the side effect logic buried in the individual accessors, you now have to store it in the universal accessor functions. If you simply want to apply universal getters / setters because thereʼs no get/set-logic that has to be abstracted, you could as well declare these variables public and use them directly.
Yes:
Originally Posted by elija
Note that boxing and unboxing is neccessary in this approach. However it requires string comparisons for each call of «get()» and thus performs poorly. I do not know if there are solutions that perform better.Code:import java.lang.reflect.Field; import java.lang.reflect.Type; public class test { /* …… Private vars …… */ private int var1; private char var2; private long var3; private Integer var4; private String var5; /* …… Public functions …… */ public void set_vars() { var1 = 42; var2 = 41; var3 = 40; var4 = Integer.valueOf(39); var5 = "Hello world"; } public Object get(String var_name) throws Exception { System.out.print("get() called: "); Field f = this.getClass().getDeclaredField(var_name); Type t = f.getGenericType(); String type_name = t.toString(); if (type_name.equals("boolean")) { return Boolean.valueOf(f.getBoolean(this)); } else if (type_name.equals("byte")) { return Byte.valueOf(f.getByte(this)); } else if (type_name.equals("char")) { return Character.valueOf(f.getChar(this)); } else if (type_name.equals("double")) { return Double.valueOf(f.getDouble(this)); } else if (type_name.equals("float")) { return Float.valueOf(f.getFloat(this)); } else if (type_name.equals("int")) { return Integer.valueOf(f.getInt(this)); } else if (type_name.equals("long")) { return Long.valueOf(f.getLong(this)); } else if (type_name.equals("short")) { return Short.valueOf(f.getShort(this)); } else { return f.get(this); } } public Object set(String var_name, Object var_value) { /* Analogous to the universal getter; use the functions * Field.set*(Object obj, * value) * and * Field.set(Object obj, Object value) * as seen on http://java.sun.com/javase/6/docs/api/java/lang/reflect/Field.html */ return null; } /* …… Main …… */ public static void main(String[] argv) { test b = new test(); b.set_vars(); try { System.out.println(b.get("var1")); System.out.println(b.get("var2")); System.out.println(b.get("var3")); System.out.println(b.get("var4")); System.out.println(b.get("var5")); } catch (Exception e) { System.out.println("OMG, no! An exception!"); } } };
- 03-09-2009 #3
The advantage is that you can over-ride them in the class itself to provide any functionality you need. The PHP 5 magic methods are a last resort before an error is thrown by the PHP compiler. You just don't have to write them and as a lazy person I appreciate that immensely

Also if the attributes are not declared as public, it becomes trivial to add a specific accessor / mutator at a later stage if it becomes required as long as you don't change the interface.
I'll take a look through your code and see what I think but I'm fairly certain I won't be doing this in Java after all as everything I have seen online seems to indicate that reflection in Java performs quite poorly. The PHP reflection API by contrast performs quite well.If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.


Reply With Quote
