php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24543 private/protected class member inconsistent behavior
Submitted: 2003-07-08 13:23 UTC Modified: 2003-08-10 18:48 UTC
From: dafrancesch at yahoo dot com dot ar Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5CVS-2003-07-08 (dev) OS: Win2000
Private report: No CVE-ID: None
 [2003-07-08 13:23 UTC] dafrancesch at yahoo dot com dot ar
Description:
------------
Accessing private or protected members from outside a class generate errors like:
Fatal error: Cannot access private property a::$vProtected in C:\www\test\php5\class.php on line xx
While accessing a private member of A from B does not. (see the example for classes A and B)


I'm using the the Win32 snapshot 200307081030


Reproduce code:
---------------
http://www.agendamagica.com.ar/php5/class.php.html

Expected result:
----------------
I expect one of these situations:

1. A warning when you try to access a private member of A from B, and when you try to access private or protected member form outside the class.

[[ Error in cosntructor of B, accessing A::vPrivate1 ]]
A::show()
::vPublic = [Set in B]
::vProtected = [Set in B]
::vPrivate1 = [Set in A]
::vPrivate2 = [Set in A]

B::show()
::vPublic = [Set in B]
::vProtected = [Set in B]
::vPrivate1 = [Set in B]  <<-- Error accessing A::vPrivate1
::vPrivate2 = []          <<-- Error accessing A::vPrivate2
::vPrivate3 = [Set in B]

Show $vB
vB->vAny = []
vB->vPublic = [Set in B]
vB->vPrivate1 = [Set in B] <<-- Error accessing A::vPrivate1
vB->vPrivate2 = []         <<-- Error accessing A::vPrivate2
vB->vPrivate3 = []         <<-- Error accessing B::vPrivate3
vB->vProtected = []       <<-- Error accessing A::vProtected


2. the actual behavior inside a class, and display nothing from outside the class (as described in http://www.php.net/zend-engine-2.php)

A::show()
::vPublic = [Set in B]
::vProtected = [Set in B]
::vPrivate1 = [Set in A]
::vPrivate2 = [Set in A]

B::show()
::vPublic = [Set in B]
::vProtected = [Set in B]
::vPrivate1 = [Set in B]
::vPrivate2 = []
::vPrivate3 = [Set in B]

Show $vB
vB->vAny = []
vB->vPublic = [Set in B]
vB->vPrivate1 = [Set in B]
vB->vPrivate2 = []
vB->vPrivate3 = []
vB->vProtected = []


The first one is preferable, and is likely other OO Lenguages.


Actual result:
--------------
A::show()
::vPublic = [Set in B]
::vProtected = [Set in B]
::vPrivate1 = [Set in A]
::vPrivate2 = [Set in A]

B::show()
::vPublic = [Set in B]
::vProtected = [Set in B]
::vPrivate1 = [Set in B]
::vPrivate2 = []
::vPrivate3 = [Set in B]

Show $vB
vB->vAny = []
vB->vPublic = [Set in B]
vB->vPrivate1 = [Set in B]
vB->vPrivate2 = []

Fatal error: Cannot access private property b::$vPrivate3 in C:\www\test\php5\class.php on line 53


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-10 18:48 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You're wrong about other languages.

A derived class knows nothing about private members of inherited classes.

Now reconsider your expectations.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 15:01:32 2024 UTC