# Access of protected members

- Thread: https://externals.io/message/87565
- Original archive: https://news-web.php.net/php.internals/87565
- Started by Nicolai Scheer on 2015-08-03T09:39:05+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #87565 — Nicolai Scheer — 2015-08-03T09:39:05+00:00

URL: https://externals.io/message/87565#87565

Hi,

just stumbled upon a strange issue.
I always thought that protected/private member variables can only be
altered from inside the object.

This example shows, that this is not true:

class Test
{
    protected $member = null;

    public static function getObj()
    {
        $myself = new Test();
        $myself->member = "hello world";
        return $myself;
    }
}

$new_object = Test::getObj();
var_dump( $new_object );

The output is:
object(Test)#1 (1) {
  ["member":protected]=>
  string(11) "hello world"
}

Of course, I'm "inside" the right class, nevertheless, the object
stored in $myself should not allow direct access to its members.

Is this the expected behaviour? Code of this kind is used quite
frequently for factory methods.

Greetings

Nico

---

## #87567 — Rowan Collins — 2015-08-03T10:36:02+00:00

URL: https://externals.io/message/87565#87567 · In reply to: #87565

>Hi,
>
>just stumbled upon a strange issue.
>I always thought that protected/private member variables can only be
>altered from inside the object.

Yeah, as far as I know, PHP has always interpreted private and protected as "available to this class" rather than "available to this instance". I'm not sure why, but it's probably not something that can be changed at this stage.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #87573 — Christian Stoller — 2015-08-03T12:02:38+00:00

URL: https://externals.io/message/87565#87573 · In reply to: #87565

From: Nicolai Scheer [mailto:nicolai.scheer@gmail.com], Sent: Monday, August 03, 2015 11:39 AM
> 
> Hi,
>
> just stumbled upon a strange issue.
> I always thought that protected/private member variables can only be
> altered from inside the object.
>
> This example shows, that this is not true:
> [...]
> Of course, I'm "inside" the right class, nevertheless, the object
> stored in $myself should not allow direct access to its members.
>
> Is this the expected behaviour? Code of this kind is used quite
> frequently for factory methods.
>

This is intended behavior. It is even possible to access private
methods and properties of another instance from the same type.
See example #3 on http://php.net/manual/en/language.oop5.visibility.php

The reason why this is possible is given, too:
> Objects of the same type will have access to each others
> private and protected members even though they are not the
> same instances. This is because the implementation specific
> details are already known when inside those objects.


Best reagrds
Christian

---

## #87581 — Stephen Coakley — 2015-08-03T16:17:37+00:00

URL: https://externals.io/message/87565#87581 · In reply to: #87565

> Hi,
>
> just stumbled upon a strange issue.
> I always thought that protected/private member variables can only be
> altered from inside the object.
>
> This example shows, that this is not true:
>
> class Test
> {
>      protected $member = null;
>
>      public static function getObj()
>      {
>          $myself = new Test();
>          $myself->member = "hello world";
>          return $myself;
>      }
> }
>
> $new_object = Test::getObj();
> var_dump( $new_object );
>
> The output is:
> object(Test)#1 (1) {
>    ["member":protected]=>
>    string(11) "hello world"
> }
>
> Of course, I'm "inside" the right class, nevertheless, the object
> stored in $myself should not allow direct access to its members.
>
> Is this the expected behaviour? Code of this kind is used quite
> frequently for factory methods.
>
> Greetings
>
> Nico
>

This is correct behavior. This is the same behavior that most 
object-oriented languages have, including Java and C#.

-- 
Stephen Coakley

---

## #87584 — Stanislav Malyshev — 2015-08-03T19:43:37+00:00

URL: https://externals.io/message/87565#87584 · In reply to: #87565

Hi!

> just stumbled upon a strange issue.
> I always thought that protected/private member variables can only be
> altered from inside the object.

No, it's not correct. Private members are visible withing this class'
scope, protected members are visible within this class' hierarchy.

The reason is that private is considered a detail of implementation of
particular class (thus visible to this class only), and protected is a
member of this class internal API (i.e. visible to extending classes but
not to external users of the class).
-- 
Stas Malyshev
smalyshev@gmail.com
