Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat does `protected` mean in PHP?
    primarykey
    data
    text
    <p>I was reading through some PHP code, and I noticed a class calling a protected method on its sibling (inherited from a common parent). This strikes me as counter-intuitive.</p> <p>Does <code>in the same inheritance tree</code> in PHP mean something <a href="https://stackoverflow.com/a/215505/12570">like Java's <code>part of the same package</code></a> ? </p> <p><a href="http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx" rel="nofollow noreferrer">I'm more used to the C# meaning of <code>protected</code>.</a></p> <p>Because I am more used to C#'s meaning of protected, I was not expecting to be able to call the protected method on a sibling class. In Java, the distinction is clear from the package. Is there anything, other than inheritance, that defines accessibility in this instance in PHP?</p> <pre><code>&lt;? class C1 { protected function f() { echo "c1\n"; } } class C2 extends C1 { protected function f() { echo "c2\n"; } } class C3 extends C1 { public function f() { // Calling protected method on parent. $c1 = new C1(); $c1 -&gt; f(); // Calling protected method on sibling??!? $c2 = new C2(); $c2 -&gt; f(); echo "c3\n"; } } $c3 = new C3(); $c3 -&gt; f(); // OUTPUT: // c1 // c2 // c3 </code></pre> <p>Here's me trying to the same thing in C# (and failing).</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class c1 { protected void f() { Console.WriteLine("c1"); } } class c2: c1 { protected void f() { Console.WriteLine("c2"); } public void g() { Console.WriteLine("g!"); } } class c3 : c1 { protected void f() { // Error 1 Cannot access protected member 'ConsoleApplication2.c1.f()' // via a qualifier of type 'ConsoleApplication2.c1'; the qualifier must be // of type 'ConsoleApplication2.c3' (or derived from it) //c1 cone = new c1(); //cone.f(); base.f(); c2 ctwo = new c2(); //Error 1 'ConsoleApplication2.c2.f()' is inaccessible due to its protection level ctwo.f(); ctwo.g(); Console.WriteLine("c3"); } } class Program { static void Main(string[] args) { c3 cthree = new c3(); // Error 2 'ConsoleApplication2.c3.f()' is inaccessible due to its protection level cthree.f(); } } } </code></pre> <hr> <p>It looks like the behaviour expected was the case prior to PHP 5.2. <a href="https://wiki.php.net/rfc/protectedlookup" rel="nofollow noreferrer">This RFC explains the issue a little more</a>, and points to <a href="https://bugs.php.net/bug.php?id=37632" rel="nofollow noreferrer">why the change happened in this bug</a>.</p> <p>I'm not sure it quite answers my question, but I thought I'd update the question, in case it helps anyone.</p> <p>Thanks to Robin F., for <a href="http://marc.info/?t=120221202200001&amp;r=1&amp;w=2" rel="nofollow noreferrer">pointing me to this discussion of the RFC</a>, for some background.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload