How does PHP obtain the protected attribute?

Source: Internet
Author: User
How does PHP obtain the protected attribute? Class userVo extends model {
Protected $ userid = array (.....);

Protected $ username = array (.....);

.........
}


Class model {

Public function getVoProtectedProperties (){

How to obtain the protected attribute in userVo (object rather than class?
$ This points to the subclass instance
}
}



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 you directly obtain the values of all the protected attribute fields of the object.


Reply to discussion (solution)


Using magic means, I 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;
}

}

You can add a getProp method to the userVo class without reflection.

Public function getProp (){
Return get_class_vars (get_class ($ this ));
}

No reflection required. the protected and private attributes cannot be obtained outside the class.

Get_class_vars () cannot get the field modifier protected.


The field name cannot be determined. What I want is the protected field of any VO class. So the current method is:

$ Class_date = new ReflectionClass (get_class ($ this ));
$ Properties = $ class_date-> getProperties (ReflectionProperty: IS_PROTECTED );


But I always think: why cannot I directly obtain the value of the protected field from the object?
Is it for security reasons?

If you do not want to use reflection, add an array for each VO class. the array content is the attribute name of protected and the array is directly returned.

Attribute names can only be returned by reflection. in programming, attribute names should be named. for the purpose of your design, attribute names are not named, it depends on some logic. Is there any design defect?

 'Id', 'username' => 'username ',); // this object can be replaced by $ this-> prote ['userid'] instead of $ this-> userid} class model {protected $ prote = null; public function getVoProtectedProperties () {if (is_array ($ this-> prote) {return array_keys ($ this-> prote) ;}} class Action {public function init () {$ uservo = new userVo (); $ uservo-> getVoProtectedProperties (); // If you want to use $ uservo-> userid, you can use magic to implement it }}

The protected protection mode is accessible only in the class itself and in the derived class.

Your application reads the protected data of sub-classes in the parent class, which itself is out of order (reflection is too different)

If you want to transmit data between classes, we recommend that you set a single-instance mode context class as a carrier.


The parent class reads the data of the subclass. it is because there are several subclasses that inherit from the parent class. The parent class is used for parsing, and the parsed VO is handed over to the presentation layer for processing.

The purpose is to: configure a VO and add, delete, modify, and query all operations automatically. Therefore, attributes in VO can only be dynamically obtained for resolution.
The parent class not only needs to read the data of the subclass, but also can tamper with or supplement the attributes of the subclass.

I feel that PHP is very dynamic and suitable for quick development platforms.

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.