How PHP Gets the protected property

Source: Internet
Author: User
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 ',
);

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.