A member permissions puzzle in PHP

Source: Internet
Author: User
Problem: The following code is available:
Class far
{
protected $arr;

protected function init () {
foreach ($this->arr as $k = + $val) {
$this $k = $val;
}
}

Public Function __construct () {
$this->init ();
}

Public Function __set ($name, $val) {
$this $name = $val;
}
}

Class Son extends Far
{
protected $a;

Public Function __construct () {
$this->arr = Array (
' A ' = ' 1 ',
);

Parent::__construct ();
}
}

$obj = new Son ();
Print_r ($obj);
Q: Why is the result of $obj output, a not 1, but null.
Son Object
(
[A:protected] = 1
[arr:protected] = = Array
(
[A] = 1
)

)

Question 2: If you change the private $a of the subclass to protected $a or public $a in the above code, the output is:
Son Object
(
[A:protected] = 1
[arr:protected] = = Array
(
[A] = 1
)

[BB] = 1
)

Why?


Reply to discussion (solution)

I don't know what you're talking about.
Didn't you print the correct result?

To correct this, the results of the private printing are as follows:

Son Object (    [a:son:private] =     [arr:protected] = = Array        (            [A] = 1        )    [BB] + 1)

Son::a is private, far:: Init cannot access him

Son::a is private, far:: Init cannot access him



Isn't there a __set magic method?

__set not far?

__set not far?



Didn't the subclass inherit it?
$Son->BB = 1;
To prove that son got the far __set.

Yes, the method is inherited, but the permissions are not changed by inheritance

My wife is yours, and I can't say that you can touch it.

Yes, the method is inherited, but the permissions are not changed by inheritance

My wife is yours, and I can't say that you can touch it.


Yes, then the $this in __set is not the current instance object, which is an instance of son. And __set's role is to be able to access private or non-existent members,

Not complete Ah, looked for a long day, all? See $BB.

Not complete Ah, looked for a long day, all? See $BB.


Sorry, $BB is my external addition, as follows:
$obj = new Son (), $obj->bb = 1;print_r ($obj);

Private is a proprietary property that can only be called internally, and cannot be invoked by an instance.
Of course far can not be assigned, normal.

Private is a proprietary property that can only be called internally, and cannot be invoked by an instance.
Of course far can not be assigned, normal.



Well, I mean, __set, the __get method is actually a way to access a member that doesn't exist or is privately private. The manual shows the following:
"__set () is called when a value is assigned to an unreachable property. ”

See also: http://www.php.net/manual/zh/language.oop5.overloading.php

Your __set method is defined in the far, so he cannot access Son's private property
That's all you have to say.

Class Far {    protected $arr;    protected function init () {        foreach ($this->arr as $k = + $val) {            $this $k = $val;        }    }    Public Function __construct () {        $this->init ();    }    Public Function __set ($name, $val) {        $this, $name = $val;    }} Class Son extends far {    private $a;    Public Function __construct () {        $this->arr = Array (            ' a ' = ' 1 ',        );        Parent::__construct ();    }    Public Function __set ($name, $val) {        $this, $name = $val;    }} $obj = new Son ();p rint_r ($obj);
Son Object
(
[A:son:private] = 1
[arr:protected] = = Array
(
[A] = 1
)

)

Your __set method is defined in the far, so he cannot access Son's private property
That's all you have to say.

Class Far {    protected $arr;    protected function init () {        foreach ($this->arr as $k = + $val) {            $this $k = $val;        }    }    Public Function __construct () {        $this->init ();    }    Public Function __set ($name, $val) {        $this, $name = $val;    }} Class Son extends far {    private $a;    Public Function __construct () {        $this->arr = Array (            ' a ' = ' 1 ',        );        Parent::__construct ();    }    Public Function __set ($name, $val) {        $this, $name = $val;    }} $obj = new Son ();p rint_r ($obj);
Son Object
(
[A:son:private] = 1
[arr:protected] = = Array
(
[A] = 1
)

)



Yes, I write in the parent class, just do not want to write a __set in each subclass, as you say is no problem. What I don't understand, though, is that it can get the __set method from the parent class. With the __set method written in its own interior, there is nothing different in the operation of the member when the inverted bottom.

Private is inviolable.
Savor the second sentence of #

Private is inviolable.
Savor the second sentence of #



One thing I don't understand is that if I print the current $this in __set, here's what I get is a subclass object. Then $this->name is not the method of the subclass object to manipulate its own members? But there's no right here.

How many times do you have to say you can turn the corner?
A private property that can only be accessed by a method defined by his class
Notice the difference between classes and objects

How many times do you have to say you can turn the corner?
A private property that can only be accessed by a method defined by his class
Notice the difference between classes and objects



If you say so, I will understand.
  • 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.