How does PHP get the protected property?
Class Uservo extends Model {
Protected $userid = Array (...);
protected $username = Array (...);
.........
}
Class model{
Public Function getvoprotectedproperties () {
How do I get the protected property in Uservo (object instead of Class)?
$this point to subclass instances
}
}
Class Action {
Public Function init () {
$uservo = new Uservo ();
$uservo->getvoprotectedproperties ();
}
}
-------------------------
Do not want to use: $class _date = new Reflectionclass (Get_class ($this)); To get the attribute.
Can not directly get the value of the field of all protected properties of the object.
Share to:
------Solution--------------------
With the Magic method, don't know if it's what you want.
Class Uservo extends Model {
Protected $userid = Array (...);
protected $username = Array (...);
Private Function __get ($property _name)
{
if (Isset ($this-$property _name))
{
Return ($this, $property _name);
}else
{
return (NULL);
}
}
Private Function __set ($property _name, $value)
{
$this $property _name = $value;
}
}
------Solution--------------------
You can add a method to the Uservo class without reflection Getprop
Public Function Getprop () {
Return Get_class_vars (Get_class ($this));
}
Without reflection, the protected and private properties are not obtained outside the class.
------Solution--------------------
If you do not want to use reflection, each VO class add more arrays, the contents of the array is the protected property name, directly return the array.
The property name itself can only be returned by reflection, according to the idea of programming, the attribute name should be named, and according to the purpose of your design, the property name is not named, but also rely on a certain logic to obtain, this itself is not the existence of design flaws?
Class Uservo extends Model {
Protected $prote = Array (
' UserID ' = ' id ',
' username ' = ' username ',
);