How does PHP get the protected property?

Source: Internet
Author: User
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.


Reply to discussion (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;
}

}

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.

Get_class_vars () has no way to get the modifier qualifier protected for the field.


Because the name of the field cannot be determined. What I want is any protected field of the Vo class. So the current approach is:

$class _date = new Reflectionclass (Get_class ($this));
$properties = $class _date->getproperties (reflectionproperty::is_protected);


But I always think: why can't we get the value of the protected field directly from the object?
Is it considered for security reasons?

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?

 
  ' id ', ' username ' = ' username ', '//And this object can be used entirely in place of $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 here, you can use magic method to implement}}

Protected protected mode is accessible only in the class itself and in derived classes

Your application is to read the protected data of subclasses in the parent class, which is in itself irregular (with reflection too alternative)

If you need to pass data between classes, it is recommended that you set up a single-mode context class as a vector


The parent reads the data for the subclass: it is because there are several subclasses that inherit from the parent class. The unity is resolved by the parent class, and then the parsed Vo is handed to the presentation layer for processing.

The purpose is to want to: Configure a VO, delete and change all automatic fix. So the attributes within VO can only be retrieved dynamically to parse.
The parent class not only reads the data of the subclass, but also the parent class is able to tamper with or supplement the properties of the subclass.

Feel PHP is very dynamic, suitable for fast development platform.

  • Related Article

    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.